安卓android四种事件监听方式的基础代码

内部类bt1   内部匿名类bt2:

public class MainActivity extends AppCompatActivity {
    Button bt1;
    Button bt2;
    Button bt3;
    Button bt4;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = findViewById(R.id.button1);
        tv = findViewById(R.id.textView);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("按钮1触发了");
            }
        });
        bt2 = findViewById(R.id.button2);
        bt2.setOnClickListener(new clicklistener());
    }
    class clicklistener implements View.OnClickListener{
        public void onClick(View v)
        {
            tv.setText("按钮2触发了");
        }
    }
}

 

你可能感兴趣的:(android,开发)