阅读背景:

一个监听器绑定多个按钮控件

来源:互联网 
 
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IndexActivity extends AppCompatActivity {

    private Button btnS01E01;
    private Button btnS01E02;
    private Button btnS01E03;
    private Button btnS01E04;
    private Button btnS01E05;
    private Button btnS01E06;

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

        btnS01E01 = (Button)findViewById(R.id.btnS01E01);
        btnS01E02 = (Button)findViewById(R.id.btnS01E02);
        btnS01E03 = (Button)findViewById(R.id.btnS01E03);
        btnS01E04 = (Button)findViewById(R.id.btnS01E04);
        btnS01E05 = (Button)findViewById(R.id.btnS01E05);
        btnS01E06 = (Button)findViewById(R.id.btnS01E06);

        btnS01E01.setOnClickListener(new ButtonListener());
        btnS01E02.setOnClickListener(new ButtonListener());
        btnS01E03.setOnClickListener(new ButtonListener());
        btnS01E04.setOnClickListener(new ButtonListener());
        btnS01E05.setOnClickListener(new ButtonListener());
        btnS01E06.setOnClickListener(new ButtonListener());
    }

    class ButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnS01E01:
                    startActivity(new Intent(IndexActivity.this, S01E01Activity.class));
                    break;
                case R.id.btnS01E02:
                    startActivity(new Intent(IndexActivity.this, S01E02Activity.class));
                    break;
                case R.id.btnS01E03:
                    startActivity(new Intent(IndexActivity.this, S01E03Activity.class));
                    break;
                case R.id.btnS01E04:
                    startActivity(new Intent(IndexActivity.this, S01E04Activity.class));
                    break;
                case R.id.btnS01E05:
                    startActivity(new Intent(IndexActivity.this, S01E05Activity.class));
                    break;
                case R.id.btnS01E06:
                    startActivity(new Intent(IndexActivity.this, S01E06Activity.class));
                    break;
            }
        }
    }
}import android.content.Intent;
import androi



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: