從零開始學android開發-View的setOnClickListener的添加方法

來源:互聯網
上載者:User

標籤:

1)第一種,也是最長見的添加方法(一下都以Button為例)

Button btn = (Button) findViewById(R.id.myButton); btn .setOnClickListener(new View.OnClickListener() {         public void onClick(View v) { //do something         }     });

2)第二種,下面這個方法較前一種稍微簡單了一些,允許多個Buttons共用一個Listener。通過Switch控制對不同Button Click事件的回應程式法:

Button btn = (Button) findViewById(R.id.mybutton);Button btn2 = (Button) findViewById(R.id.mybutton2);btn.setOnClickListener(handler);btn2.setOnClickListener(handler);View.OnClickListener handler = View.OnClickListener() {        public void onClick(View v) {            switch (v.getId()) {               case R.id.mybutton: //do something               break;               case R.id.mybutton2: //do something               break;            }    }

或者

    Button list=null;    Button about=null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.main);        list=(Button)findViewById(R.id.foodlistbtn);        about=(Button)findViewById(R.id.aboutbutton);//        list.setText(text);//        list.setCompoundDrawables(left, top, right, bottom);        list.setOnClickListener(this);        about.setOnClickListener(this);            }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        if(v.getId()==R.id.foodlistbtn){        Intent intent=new Intent();        intent.setClass(MainApp.this, FoodListView.class);        startActivity(intent);        list.setBackgroundResource(R.drawable.btn_food_list_active);        }else if(v.getId()==R.id.aboutbutton){            Intent intent=new Intent(this, About.class);            startActivity(intent);            about.setBackgroundResource(R.drawable.btn_food_about_active);        }    }

3)第三種,直接將Clicklistener捆綁XML layout中的Views元素,在程式中定義的Listener方法需要帶有一個View類型的參數:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView android:layout_width="fill_parent"        android:layout_height="wrap_content" android:id="@+id/text"        android:text="@string/hello" />    <Button android:id="@+id/mybutton" android:layout_height="wrap_content"        android:layout_width="wrap_content" android:onClick="mybuttonlistener"></Button></LinearLayout>

java代碼:

 Button btn = (Button) findViewById(R.id.mybutton);  public void mybuttonlistener(View target){ //do something     }

 

從零開始學android開發-View的setOnClickListener的添加方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.