初学android studio——如何同时监听多个button实现activity跳转

1.首先在layout布局文件中创建多个按钮并设置相应的id,并在.java文件下声明这些按钮

public class MainActivity extends AppCompatActivity {
    Button btn_linechart;
    Button btn_barchart, btn_horizonalbarchart;
    Button btn_combinedchart;
    Button btn_piechart;
    Button btn_scatterchart;
    Button btn_candlechart;
    Button btn_radarchart;
    Intent a, b, c, d, e, f, g, h;

2.在onCreat函数中把他们连接起来

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_linechart = (Button) findViewById(R.id.btn_linechart);
        btn_linechart.setOnClickListener(new ButtonListener());

        btn_barchart = (Button) findViewById(R.id.btn_barchart);
        btn_barchart.setOnClickListener(new ButtonListener());

        btn_horizonalbarchart = (Button) findViewById(R.id.btn_horizontalchart);
        btn_horizonalbarchart.setOnClickListener(new ButtonListener());

        btn_combinedchart = (Button) findViewById(R.id.btn_combinedchart);
        btn_combinedchart.setOnClickListener(new ButtonListener());

        btn_piechart = (Button) findViewById(R.id.btn_piechart);
        btn_piechart.setOnClickListener(new ButtonListener());

        btn_scatterchart = (Button) findViewById(R.id.btn_scatterchart);
        btn_scatterchart.setOnClickListener(new ButtonListener());

        btn_candlechart = (Button) findViewById(R.id.btn_candlechart);
        btn_candlechart.setOnClickListener(new ButtonListener());

        btn_radarchart = (Button) findViewById(R.id.btn_radarchart);
        btn_radarchart.setOnClickListener(new ButtonListener());
    }

3.声明Intent变量

public class MainActivity extends AppCompatActivity {
    Button btn_linechart;
    Button btn_barchart, btn_horizonalbarchart;
    Button btn_combinedchart;
    Button btn_piechart;
    Button btn_scatterchart;
    Button btn_candlechart;
    Button btn_radarchart;
    Intent a, b, c, d, e, f, g, h;

4.使用switch、case实现监听作用

private class ButtonListener implements View.OnClickListener {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_linechart:
                    a = new Intent(MainActivity.this, LineChartActivity.class);
                    startActivity(a);
                    break;
                case R.id.btn_barchart:
                    b = new Intent(MainActivity.this, BarChartActivity.class);
                    startActivity(b);
                    break;
                case R.id.btn_horizontalchart:
                    c = new Intent(MainActivity.this, HorizontalChart.class);
                    startActivity(c);
                    break;
                case R.id.btn_combinedchart:
                    d = new Intent(MainActivity.this, CombinedChartActivity.class);
                    startActivity(d);
                    break;
                case R.id.btn_piechart:
                    e = new Intent(MainActivity.this, PieChartActivity.class);
                    startActivity(e);
                    break;
                case R.id.btn_scatterchart:
                    f = new Intent(MainActivity.this, ScatterChartActivity.class);
                    startActivity(f);
                    break;
                case R.id.btn_candlechart:
                    g = new Intent(MainActivity.this, CandleChart.class);
                    startActivity(g);
                    break;
                case R.id.btn_radarchart:
                    h = new Intent(MainActivity.this, RadarChartActivity.class);
                    startActivity(h);
                    break;
                    default:
                        break;
            }
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值