1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > view.performClick()触发点击事件

view.performClick()触发点击事件

时间:2020-01-08 14:40:01

相关推荐

view.performClick()触发点击事件

1、主要作用

自动触发控件的点击事件

2、界面的布局文件 activity_main.xml

<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/bt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="点击一下" /><Buttonandroid:id="@+id/bt2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/bt"android:text="@string/hello_world" /></RelativeLayout>

3、MainActivity 代码

package com.android;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {Button button1 ;Button button2 ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main );button1 = (Button) findViewById( R.id.bt ) ; button1.setOnClickListener( new OnClickListener() {@Overridepublic void onClick(View v) {button2.performClick(); //调用 button2的点击事件 }});button2 = (Button) findViewById( R.id.bt2 ) ;button2.setOnClickListener( new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText( MainActivity.this , "点击了", Toast.LENGTH_SHORT).show();}});}}

4、注意事项

如果同时使用了view.setOnTouchListener()方法,则有可能存在拦截view.performClick()的响应事件,

因为当view.OnTouchEvent()在event.getAction() ==MotionEvent.ACTION_DOWN时返回false,

系统会认为view不需要处理Touch事件,则后续的Touch事件(move、up、click)就不会被传进来 。

所以也不会触发view.performClick(),而view.setOnTouchListener()相当于是重写了view.OnTouchEvent(),

所以在写view的TouchListener处理时,需要留意view是否存在点击事件监听,如果有,则在适当的位置使用view.performClick()触发点击事件。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。