Android为按钮Button添加事件

匿名内部类

1 <!--匿名内部类方式-->
2     <Button
3         android:id="@+id/btn2"
4         android:layout_width="wrap_content"
5         android:layout_height="wrap_content"
6         android:textSize="30sp"
7         android:text="匿名内部类方式"
8         />
 1 public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
 2     @Override
 3     protected void onCreate(Bundle savedInstanceState) {
 4         super.onCreate(savedInstanceState);
 5         setContentView(R.layout.activity_btn);
 6         final Button btn2;
 7         final EditText ed01=findViewById(R.id.ed01);
 8         final EditText ed02=findViewById(R.id.ed02);
 9         btn2 = findViewById(R.id.btn2);
10         btn2.setOnClickListener(new View.OnClickListener() {
11                                     public void onClick(View v) {
12                                         Log.i("MainActivity", "匿名内部类按钮被点击");
13                                         Log.i("ed01", ed01.getText().toString());
14                                         Log.i("ed02", ed02.getText().toString());
15   Toast.makeText(MainActivity.this,"这是显示的内容",Toast.LENGTH_SHORT).show();
16                                     }
17                                 });

 

posted @ 2021-01-31 21:31  靠谱杨  阅读(236)  评论(0编辑  收藏  举报