初学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;
            }
        }
    }

你可能感兴趣的:(初学android studio——如何同时监听多个button实现activity跳转)