在android中button響應的幾種方式

在android中button響應的幾種方式

轉自 http://blog.csdn.net/abc5382334/article/details/9980637

1.

在佈局文件中添加button的監聽名字

Android:onClick=”buttonOnClick”

例如:

<Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/button1"

        android:layout_below="@+id/textView2"

        android:layout_marginTop="28dp"

        android:text="Button"

        android:onClick="buttonOnClick" />

 

在activity中創建響應的函數

publicvoid buttonOnClick(View view){

      Mytext1.setText("hello!");//設置文檔的顯示

   }

 

2.

在OnCreate函數中寫

Mybutton = (Button)findViewById(R.id.button1);

Mybutton.setOnClickListener(new OnClickListener(){

         @Override

         publicvoid onClick(View v) {

         // TODO Auto-generated method stub

            Mytext.setText("hello!");//設置文檔的顯示

         }

         });

 

 

3.在OnCreate函數中寫

 

findViewById(R.id.btn_title_popmenu).setOnClickListener(this);

findViewById(R.id.button1).setOnClickListener(this);

 

再來

publicvoid onClick(View v) {

      if(v.getId() == R.id.btn_title_popmenu){

         popMenu.showAsDropDown(v);

      }elseif(v.getId()==R.id.button1)

      {

         popMenu1.showAsDropDown(v);

      }

   }

前提條件是:

public class MainActivity extends Activity implements OnClickListener{}

 

第三種方法例子:

publicclass MainActivity extends Activity implements OnClickListener{

   private TextView Mytext = null;

   private TextView Mytext1 = null;

   @Override

   //第一次運行activity時會運行

   protectedvoid onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);//設置要顯示的控件

      findViewById(R.id.button1).setOnClickListener(this);

      findViewById(R.id.button2).setOnClickListener(this);

      Mytext = (TextView)findViewById(R.id.textView1);

      Mytext1 = (TextView)findViewById(R.id.textView2);

      }

  

 

      @Override

      publicvoid onClick(View arg0) {

         // TODO自動生成的方法存根

         if(arg0.getId()==R.id.button1){

            Mytext.setText("hello!");//設置文檔的顯示

         }

         elseif(arg0.getId()==R.id.button2){

            Mytext1.setText("hello!");//設置文檔的顯示

         }

      }

 

前兩種方法的例子:

public class MainActivity extends Activity {

   private Button Mybutton = null;

   private Button Mybutton1 = null;

   private TextView Mytext = null;

   private TextView Mytext1 = null;

   @Override

   //第一次運行activity時會運行

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);//設置要顯示的控件

      Mybutton = (Button)findViewById(R.id.button1);

      Mytext = (TextView)findViewById(R.id.textView1);

      Mytext1 = (TextView)findViewById(R.id.textView2);

//第二種方法

      Mybutton.setOnClickListener(new OnClickListener(){

         @Override

         public void onClick(View v) {

          //TODO Auto-generated method stub

            Mytext.setText("hello!");//設置文檔的顯示

         }

         });

      }

//第一種方法

   public void button2OnClick(View view){

      Mytext1.setText("hello!");//設置文檔的顯示

   }

//第一種方法要在button的佈局文件中設置android:onClick="buttonOnClick"

 

更多0
下一篇:安卓 創建一個選項菜單的方法和步驟:
發佈了5 篇原創文章 · 獲贊 6 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章