Android開發之onClick事件的三種寫法(轉)

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   sp   

 1 package a.a; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.EditText; 8  9 public class AActivity extends Activity {10     /** Called when the activity is first created. */11 12     EditText Ev1;13 14     @Override15     public void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.main);18 19         Ev1 = (EditText)findViewById(R.id.editText1);  20 21         //第一種方式  22         Button Btn1 = (Button)findViewById(R.id.button1);//擷取按鈕資源  23         Btn1.setOnClickListener(new Button.OnClickListener(){//建立監聽  24             public void onClick(View v) {  25                 String strTmp = "點擊Button01";  26                 Ev1.setText(strTmp);  27             }  28 29         });  30 31         //第二種方式  32         Button Btn2 = (Button) findViewById(R.id.button2);//擷取按鈕資源  33         Btn2.setOnClickListener(listener);//設定監聽  34 35     }36 37     Button.OnClickListener listener = new Button.OnClickListener(){//建立監聽對象  38         public void onClick(View v){  39             String strTmp="點擊Button02";  40             Ev1.setText(strTmp);  41         }  42 43     };44 45 46     //第三種方式(Android1.6版本及以後的版本中提供了)  47     public void Btn3OnClick(View view){  48         String strTmp="點擊Button03";49         Ev1.setText(strTmp);50 51     }  52 }
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical" > 6  7     <TextView 8         android:layout_width="fill_parent" 9         android:layout_height="wrap_content"10         android:text="@string/hello" />11 12     <Button13         android:id="@+id/button1"14         android:layout_width="wrap_content"15         android:layout_height="wrap_content"16         android:text="Button1" />17 18     <Button19         android:id="@+id/button2"20         android:layout_width="wrap_content"21         android:layout_height="wrap_content"22         android:text="Button2" />23     24  <Button25         android:id="@+id/button3"26         android:layout_width="wrap_content"27         android:layout_height="wrap_content"28         android:text="Button3" 29         android:onClick="Btn3OnClick"/>30 31  <EditText32      android:id="@+id/editText1"33      android:layout_width="match_parent"34      android:layout_height="wrap_content" >35 36      <requestFocus />37  </EditText>38 39 </LinearLayout>

 

Android開發之onClick事件的三種寫法(轉)

聯繫我們

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