android Butt 按鈕單擊事件的監聽


 

 

<LinearLayout 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"
    android:background="@drawable/jisuanqi"
    android:orientation="vertical"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:paddingTop="8dip" >

    <TextView
        android:id="@+id/c"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.265"
        android:background="@drawable/show" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:layout_gravity="right"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="2dip"
            android:layout_weight="1"
            android:textColor="#EAEAEA"
            android:visibility="invisible" />

        <Button
            android:id="@+id/erf"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="2dip"
            android:layout_weight="1"
            android:background="@drawable/selector_but_function"
            android:text="復制"
            android:textColor="#EAEAEA"
            android:textSize="13dip" />

        <Button
            android:id="@+id/ftgh"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="2dip"
            android:layout_weight="1"
            android:background="@drawable/selector_but_function"
            android:text="@string/xie"
            android:textColor="#EAEAEA"
            android:textSize="13dip" />

        <Button
            android:id="@+id/rfg"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="2dip"
            android:layout_weight="1"
            android:background="@drawable/selector_but_function"
            android:text="@string/lis"
            android:textColor="#EAEAEA"
            android:textSize="13dip" />
    </LinearLayout>

 

 

public class MainActivity extends Activity{
 
 //-------------------------butons 索引----------------------------------
 private int[] buttonIndex = new int[]{
   R.id.Edtymn,R.id.Emn,R.id.ling,R.id.dfr,R.id.frfg,R.id.jhn,R.id.rt,R.id.ft,R.id.ew,
   R.id.mn,R.id.k,R.id.ui,R.id.hy,R.id.wq,R.id.g,R.id.f,R.id.o,R.id.rfg,R.id.ftgh,R.id.erf  
 };
 
 //--------------------------button 按鈕事件----------------------------
 private View.OnClickListener onClickListener = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   
   Toast.makeText(MainActivity.this,"-->"+v.getId(), Toast.LENGTH_SHORT).show();
   
   //-----------------------按鈕相關的事件動作-------------------------------------
   switch (v.getId()) {

   case R.id.Edtymn: {

   }
    break;
   case R.id.Emn: {

   }
    break;
   case R.id.ling: {

   }
    break;
   case R.id.dfr: {

   }
    break;
   case R.id.frfg: {

   }
    break;
   case R.id.jhn: {

   }
    break;
   }
  }
 };

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  //----------------------獲取按鈕添加按鈕事件----------------------------
  for(int i=0;i<buttonIndex.length;i++){
   findViewById(buttonIndex[i]).setOnClickListener(onClickListener);
  }
 }


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com