Android 点击事件 4种 写法
来源:互联网 发布:linux创建多个文件 编辑:程序博客网 时间:2024/05/29 08:27
Android中点击事件的4种写法
- Android中点击事件的4种写法
- 使用匿名内部类方式
- xml 文件
- Java代码
- 使用自定义单击事件监听类实现
- xml 文件
- Java代码
- Activity实现点击事件
- xml 文件
- Java代码
- 使用xml属性androidonClick进行设置
- xml 文件
- Java代码
- 使用匿名内部类方式
使用匿名内部类方式
xml 文件
<?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="com.dodi.myapplication.MainActivity"> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="方式1"/></LinearLayout>
Java代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //第一种方式: 使用匿名内部类方式 Button btn_1 = (Button)findViewById(R.id.btn_1); btn_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast tst = Toast.makeText(MainActivity.this, "匿名内部类方式设置点击事件", Toast.LENGTH_SHORT); tst.show(); } }); }}
使用自定义单击事件监听类实现
xml 文件
<?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="com.vanm.button.MainActivity"> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1"/> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2"/></LinearLayout>
Java代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //第二种方式: 使用自定义单击事件监听类实现 Button btn_1 = (Button) findViewById(R.id.btn_1); Button btn_2 = (Button) findViewById(R.id.btn_2); btn_1.setOnClickListener(new MyClickListener()); btn_2.setOnClickListener(new MyClickListener()); } //自定义单击事件的监听类 class MyClickListener implements View.OnClickListener { @Override public void onClick(View v) { //多个按钮的点击事件通过Switch语句区分开 switch (v.getId()) { case R.id.btn_1: Toast.makeText(MainActivity.this, "使用自定义单击事件监听类实现", Toast.LENGTH_SHORT).show(); break; case R.id.btn_2: Toast.makeText(MainActivity.this, "使用自定义单击事件监听类实现", Toast.LENGTH_SHORT).show(); break; default: break; } } }}
Activity实现点击事件
xml 文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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="com.vanm.button.MainActivity"> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="方式3"/></RelativeLayout>
Java代码
//Activity实现View.OnClickListener方法,去处理点击事件public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_3 = (Button) findViewById(R.id.btn_3); //第三种方式: Activity实现点击事件-->传入this即可 btn_3.setOnClickListener(this); } @Override public void onClick(View v) { //多个按钮的点击事件通过Switch语句区分开 switch (v.getId()) { case R.id.btn_3: Toast.makeText(MainActivity.this, "Activity实现点击事件实现", Toast.LENGTH_SHORT).show(); break; default: break; } }}
使用xml属性android:onClick进行设置
xml 文件
<?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="com.vanm.xmlonclick.MainActivity"> <!--android:onClick="btnOnClick" 给按钮指定一个点击事件 值可以自定义,并且这个值将作为点击事件方法的方法名 --> <Button android:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnOnClick" android:text="按钮1"/> <!-- 可以多个按钮指定为一个方法名,通过Switch语句区分不同的按钮--> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnOnClick" android:text="按钮2"/></LinearLayout>
Java代码
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 第4中方式: 通过XMl给指定按钮定义的点击事件, * 不需要调用setOnClickListener方法,不需要findViewById * 注意 这里没有 @Override 标签 * 固定写法: 修饰符: public 公共的 * 返回值类型: void 无返回值 * 方法名: 在XML的onclick属性中设置的值 * 形式参数: View类型 * @param v : 当前点击的Button */ public void btnOnClick(View v){ /*通过不同的Id区分不同的按钮*/ switch (v.getId()){ case R.id.btn_1: Toast.makeText(MainActivity.this, "通过XMl给指定按钮定义的点击事件", Toast.LENGTH_SHORT).show(); break; case R.id.btn_2: Toast.makeText(MainActivity.this, "通过XMl给指定按钮定义的点击事件", Toast.LENGTH_SHORT).show(); break; default: break; } }}
2 0
- Android点击事件的4种写法
- Android点击事件的4种写法
- Android点击事件的4种写法
- Android 点击事件的4种写法
- Android 点击事件 4种 写法
- android--点击事件写法
- android button点击事件的4种写法
- android常用的4种点击事件写法
- Android按钮的点击事件的4种写法
- Android 响应按钮点击事件的4种写法
- Android 4中点击事件写法
- Android点击事件的四种写法
- android点击事件的四种写法
- Android点击事件的四种写法
- Android点击事件的四种写法
- android点击事件的四种写法
- Android点击事件的三种写法
- [Android] 点击事件的四种写法
- 关于分在 Linux windows 平台下对github上的文件进行克隆 上传
- 最长公共子序列问题
- 网络不稳定 网速忽高忽低,ping值忽高忽低的解决办法 无线网出现问题解决
- Android中5种Button点击事件的实现方法
- php7 安装 2
- Android 点击事件 4种 写法
- 20151204 流水账
- hadoop Job 运行错误 java.lang.OutOfMemoryError: Java heap space
- JavaMail发送多联系人多附件邮件
- 一个包含n个字符的字符串,从第m个字符开始复制成为另外一个字符串
- remoteSort =true 用在store; 和sortable:true 用在column里的一个字段上
- c语言学习
- Codeforces Round #334 D. Moodular Arithmetic(并查集)
- golang返回值为interface{}的类型判断