很多点击事件,通过方法减少使用findViewbyid
2017-03-18 08:22
417 查看
1、
初学者可能会使用到,后期学习了ButterKnife就不会使用该方法了。学习一种思想
<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:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ceshi"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ceshi"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ceshi"
android:text="按钮3" />
</LinearLayout>
2、
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ceshi(View v) {
switch (v.getId()) {
case R.id.button1:
Toast.makeText(getApplication(), "按钮1", 0).show();
break;
case R.id.button2:
Toast.makeText(getApplication(), "按钮2", 0).show();
break;
case R.id.button3:
Toast.makeText(getApplication(), "按钮3", 0).show();
break;
}
}
}
初学者可能会使用到,后期学习了ButterKnife就不会使用该方法了。学习一种思想
<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:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ceshi"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ceshi"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ceshi"
android:text="按钮3" />
</LinearLayout>
2、
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ceshi(View v) {
switch (v.getId()) {
case R.id.button1:
Toast.makeText(getApplication(), "按钮1", 0).show();
break;
case R.id.button2:
Toast.makeText(getApplication(), "按钮2", 0).show();
break;
case R.id.button3:
Toast.makeText(getApplication(), "按钮3", 0).show();
break;
}
}
}
相关文章推荐
- 通过自定义方法减少findViewById方法使用的次数
- Volley, HttpUtils,省略findViewById()和点击事件的.setOnClickListener(this);,DbUtils
- 安卓通过注解+反射简单实现代替findViewById方法
- Android中封装View提供接口供点击事件回调的方法及使用
- android中布局文件中的控件通过findviewbyid无法关联的解决方法
- Volley, HttpUtils,省略findViewById()和点击事件的.setOnClickListener(this);,DbUtils
- 活动中的 findViewById()方法总结以及Button按钮的使用
- 通过findViewById()方法从layout中获取view并进行相应的转换时提示:"Cannot cast from View to AutoCompleteTextView"的解决办法!
- android通过反射代替findViewById的过多使用
- Android中注解详解,通过注解代替findViewById方法
- android 使用泛型再也不写findViewById方法了
- Android中通过注解代替findViewById方法
- Android中通过注解代替findViewById方法
- android---------Android中通过注解代替findViewById方法
- 手写IOC注解,解决findViewById和点击事件
- 122.xUtils使用inject取代findViewById的方法填充布局
- Android中通过注解代替findViewById方法 Android 中的 IOC 框架 【ViewInject】
- android 减少findViewById 的使用
- Android开发框架xUtils简介(附有不使用findViewById的方法)
- android中布局文件中的控件通过findviewbyid无法关联的解决方法