您的位置:首页 > 其它

很多点击事件,通过方法减少使用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;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: