android Butt 按钮单击事件的监听

时间:2021-07-16 01:13:11

 

 

<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);
  }
 }