view.performClick()觸發點擊事件,performclick

來源:互聯網
上載者:User

view.performClick()觸發點擊事件,performclick

1、主要作用

     自動觸發控制項的點擊事件

2、介面的布局檔案  activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <Button        android:id="@+id/bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="點擊一下" />    <Button        android: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 ;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main );        button1 = (Button) findViewById( R.id.bt ) ;         button1.setOnClickListener( new OnClickListener() {            @Override            public void onClick(View v) {                button2.performClick();  //調用 button2的點擊事件            }        });        button2 = (Button) findViewById( R.id.bt2 ) ;        button2.setOnClickListener( new OnClickListener() {            @Override            public 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()觸發點擊事件。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.