Android中用switch区分点击是哪个按钮的写法


方法一:实现接口,获取其资源id,通过资源id,可以判断用户点击了哪个按钮了

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yichao.buttonswitchtest.MainActivity">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1" />

    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2" />

    <Button
        android:id="@+id/bt_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3" />
</LinearLayout>

MainActivity文件:

public class MainActivity extends Activity implements View.OnClickListener{
    private Button btn_1,btn_2,btn_3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_1= (Button) findViewById(R.id.bt_1);
        btn_2= (Button) findViewById(R.id.bt_2);
        btn_3= (Button) findViewById(R.id.bt_3);

        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_1:
                Toast.makeText(MainActivity.this,"这是第一个按钮",Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_2:
                Toast.makeText(MainActivity.this,"这是第二个按钮",Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_3:
                Toast.makeText(MainActivity.this,"这是第三个按钮",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

方法二:在xml文件中指定onClick属性,获取其资源id,通过资源id,可以判断用户点击了哪个按钮了 

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yichao.buttonswitchtest.MainActivity">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_click"
        android:text="按钮1" />

    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_click"
        android:text="按钮2" />

    <Button
        android:id="@+id/bt_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_click"
        android:text="按钮3" />
</LinearLayout>
MainActivity文件:
public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
    public void btn_click(View v) {
        switch (v.getId()){
            case R.id.bt_1:
                Toast.makeText(MainActivity.this,"这是第一个按钮",Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_2:
                Toast.makeText(MainActivity.this,"这是第二个按钮",Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_3:
                Toast.makeText(MainActivity.this,"这是第三个按钮",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
方法三:自定义类实现接口,获取其资源id,通过资源id,可以判断用户点击了哪个按钮了

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yichao.buttonswitchtest.MainActivity">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1" />

    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2" />

    <Button
        android:id="@+id/bt_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3" />
</LinearLayout>
MainActivity文件:

public class MainActivity extends Activity {
    private Button btn_1, btn_2, btn_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_1 = (Button) findViewById(R.id.bt_1);
        btn_2 = (Button) findViewById(R.id.bt_2);
        btn_3 = (Button) findViewById(R.id.bt_3);

        btn_1.setOnClickListener(new MyListener());
        btn_2.setOnClickListener(new MyListener());
        btn_3.setOnClickListener(new MyListener());

    }
    class MyListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_1:
                    Toast.makeText(MainActivity.this, "这是第一个按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.bt_2:
                    Toast.makeText(MainActivity.this, "这是第二个按钮", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.bt_3:
                    Toast.makeText(MainActivity.this, "这是第三个按钮", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
}



  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值