赞
踩
1,在布局实现
我们在layout文件中,给每一个用到的Button设置属性android:onClick="onClick",
然后我们在MainActivity 里面写一个onClick()方法,这里就不是重写了,因为我们没有任何继承父类和引用接口,这里的方法名可以随意取。然后写上代码逻辑。完整代码如下:
activity_main.xml文件内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="学习安卓,你准备好了吗"
- android:id="@+id/tv_android"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="准备好了"
- android:id="@+id/bt_android"
- android:onClick="Welcome"/>##设置点击事件按钮方法为Welcome
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="没有准备好"
- android:id="@+id/bt_android_1"
- android:onClick="noway"/>##设置点击事件按钮方法为noway
- </LinearLayout>
MainActivity文件如下:
- package com.unity3d.myapplication1;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
-
- /*
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }*/
- public class MainActivity extends AppCompatActivity {
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void Welcome(View view) {
-
- Toast.makeText(this, "欢迎来到安卓世界", Toast.LENGTH_SHORT).show();
- }
- public void noway(View view) {
-
- Toast.makeText(this, "bye bye bye ", Toast.LENGTH_SHORT).show();
- }
-
-
-
- }
二、接口实现
第二种方法只要引用View.OnClickListener这个接口就行,接着Button button=findViewById(R.id.button);用来声明和绑定button控件,button.setOnClickListener(this);设置button的监听器,这两者缺一不可。下面就是重写onClick()方法,一般使用switch语句,参数是view,可以根据不同id来赋予不同的点击事件,不用像上面匿名内部类那样每一个按钮都要单独设置一下点击事件。所有代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:id="@+id/button_1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="测试按键"
- android:textSize="25sp"/>
-
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="11111"
- android:textSize="25sp"/>
-
- <Button
- android:id="@+id/button_2"
- android:layout_width="match_parent"
- android:layout_height="43dp"
- android:text="ok ok ok "
- android:textSize="25sp"
-
-
- />
- </LinearLayout>
- </androidx.constraintlayout.widget.ConstraintLayout>
- package com.unity3d.myapplication1;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- import android.widget.Button;
- import android.util.Log;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- Button TestButton1, TestButton2; //创建button
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TestButton1 = findViewById(R.id.button_1); //通过id找到对应button
- TestButton2 = findViewById(R.id.button_2);
- TestButton1.setOnClickListener(new mButtonListener());
- TestButton2.setOnClickListener(new mButtonListener());
- }
- //新建mButtonListener类申明使用OnClickListener接口
- public class mButtonListener implements View.OnClickListener{
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button_1: //按键1
- Toast.makeText(MainActivity.this,"Hello world",Toast.LENGTH_LONG).show();
-
- Log.d("button", "onClick: 1");
- break;
- case R.id.button_2: //按键2
- Toast.makeText(MainActivity.this,"ok ok ok ok ",Toast.LENGTH_LONG).show();
- Log.d("button", "onClick: 2");
- break;
- default: break;
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。