Android中点击的四种写法
2016-10-08 22:04
127 查看
下面我用代码来介绍Android中点击的四种编写方法
首先,我们在res/layout/activity_main.xml文件中编写四个点击按钮
第一种:外部类,清晰,明了
第二种:内部类,一个button就要对应一个匿名类,繁琐
第三种:突然出来一个方法,很突兀
第四种:结构上是最不好的
首先,我们在res/layout/activity_main.xml文件中编写四个点击按钮
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一种方式"/> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二种方式"/> <Button android:id="@+id/bt3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三种方式"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第四种方式" android:onClick="ForthClick"/> </LinearLayout>然后在src/包名/MainActivity.java文件中进行控制
package com.ymf.onclick; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //第一种拨打方式 Button bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new MyListener()); //第二种拨打方式 Button bt2 = (Button) findViewById(R.id.bt2); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("这是第二种点击方式"); } }); //第三种拨打方式 Button bt3 = (Button) findViewById(R.id.bt3); bt3.setOnClickListener(this); } class MyListener implements OnClickListener{ @Override public void onClick(View v) { System.out.println("第一种点击方式"); } } @Override public void onClick(View v) { System.out.println("第三种点击方式啊"); } public void ForthClick(View v){ System.out.println("第四种点击方式"); } }四种点击按钮方法:
第一种:外部类,清晰,明了
第二种:内部类,一个button就要对应一个匿名类,繁琐
第三种:突然出来一个方法,很突兀
第四种:结构上是最不好的
相关文章推荐
- Android点击事件的四种写法
- android开发中点击事件的四种写法
- Android中点击事件的四种写法详解
- android四种点击事件触发写法
- android下 按钮点击 的四种写法
- Android学习(7) -- 点击事件的四种写法
- [Android] 点击事件的四种写法
- android(2):按钮点击事件的四种写法(装载)
- android点击事件的四种写法
- Android点击事件的四种写法
- Android_01_按钮点击事件的四种写法
- [Android]Java中点击事件的四种写法
- Android点击事件的四种写法
- Android的Button控件的点击事件的四种写法
- Androidx学习笔记(4)-- 事件处理的四种写法(点击事件为例)
- [android] 点击事件的四种写法
- Android笔记---点击事件的四种写法
- Android----点击事件的四种写法
- Android中点击事件的四种写法
- Android中按钮的点击事件的四种写法