Android中给button注册事件的四种方法
https://blog.csdn.net/qq_45064049/article/details/90383158
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ButtonActivity" android:orientation="vertical"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过自定义内部类实现点击事件"/> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过匿名内部类实现点击事件"/> <Button android:id="@+id/btn3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过当前Activity去实现点击事件接口"/> <Button android:id="@+id/btn4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="在xml文件中绑定" android:onClick="myClick"/> <Button android:id="@+id/btn5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="在xml文件中绑定2" android:onClick="myClick"/> </LinearLayout>
package com.example.uidemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class ButtonActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); //1.获取按钮 Button btn1 = findViewById(R.id.btn1); //点击事件:被点击时被触发的事件 MyClickListener mcl = new MyClickListener(); btn1.setOnClickListener(mcl); //2.为按钮注册点击事件监听器 //匿名内部类适用于有唯一操作的按钮 Button btn2 = findViewById(R.id.btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //在控制台输出 Log.e("TAG","==========匿名内部类==========");
Toast.makeText(MainActivity.this, "按钮被点击", Toast.LENGTH_SHORT).show();
} }); Button btn3 = findViewById(R.id.btn3); btn3.setOnClickListener(this); } @Override public void onClick(View view) { Log.e("TAG","用本类实现了OnClickListener"); } class MyClickListener implements View.OnClickListener{ @Override public void onClick(View view) { //在控制台输出一条语句 Log.e("TAG","刚刚点击的按钮时注册了内部类监听器对象的按钮"); } } //参数:被点击的控件对象 public void myClick(View v){ switch (v.getId()){ case R.id.btn4: Log.e("TAG","btn4======"); break; case R.id.btn5: Log.e("TAG","btn5======"); break; } } }
页面跳转。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.imooc.demo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="main"> </activity> <activity android:name=".TestActivity" android:launchMode="singleTop" android:label="testActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".NewActivity" android:label="newActivity"></activity> </application> </manifest>
package com.imooc.demo; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; /** * Created by renkangke . */ public class NewActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); Button newButton = findViewById(R.id.newButton); newButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(NewActivity.this, TestActivity.class); startActivity(intent); } }); } }
package com.imooc.demo; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * Created by renkangke . */ public class TestActivity extends AppCompatActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // activity与layout进行了关联 setContentView(R.layout.activity_test); // 通过findViewById将layout中的控件找出来,并转化成View final TextView textView = findViewById(R.id.titleTextView); // 打到button这个view Button button = findViewById(R.id.button); // 设置点击事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 为textView动态设置文本 textView.setText(getString(R.string.app_name)); // 跳转到一个NewActivity Intent intent = new Intent(TestActivity.this, TestActivity.class); startActivity(intent); } }); } }