Android中用switch区分点击是哪个按钮的写法
2016-10-06 14:26
246 查看
方法一:实现接口,获取其资源id,通过资源id,可以判断用户点击了哪个按钮了
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" android:orientation="vertical" 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="com.yichao.buttonswitchtest.MainActivity"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2" /> <Button android:id="@+id/bt_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3" /> </LinearLayout>
MainActivity文件:
public class MainActivity extends Activity implements View.OnClickListener{ private Button btn_1,btn_2,btn_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_1= (Button) findViewById(R.id.bt_1); btn_2= (Button) findViewById(R.id.bt_2); btn_3= (Button) findViewById(R.id.bt_3); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_1: Toast.makeText(MainActivity.this,"这是第一个按钮",Toast.LENGTH_SHORT).show(); break; case R.id.bt_2: Toast.makeText(MainActivity.this,"这是第二个按钮",Toast.LENGTH_SHORT).show(); break; case R.id.bt_3: Toast.makeText(MainActivity.this,"这是第三个按钮",Toast.LENGTH_SHORT).show(); break; } } }
方法二:在xml文件中指定onClick属性,获取其资源id,通过资源id,可以判断用户点击了哪个按钮了
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" android:orientation="vertical" 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="com.yichao.buttonswitchtest.MainActivity"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btn_click" android:text="按钮1" /> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btn_click" android:text="按钮2" /> <Button android:id="@+id/bt_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btn_click" android:text="按钮3" /> </LinearLayout>MainActivity文件:
public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void btn_click(View v) { switch (v.getId()){ case R.id.bt_1: Toast.makeText(MainActivity.this,"这是第一个按钮",Toast.LENGTH_SHORT).show(); break; case R.id.bt_2: Toast.makeText(MainActivity.this,"这是第二个按钮",Toast.LENGTH_SHORT).show(); break; case R.id.bt_3: Toast.makeText(MainActivity.this,"这是第三个按钮",Toast.LENGTH_SHORT).show(); break; } } }方法三:自定义类实现接口,获取其资源id,通过资源id,可以判断用户点击了哪个按钮了
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" android:orientation="vertical" 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="com.yichao.buttonswitchtest.MainActivity"> <Button android:id="@+id/bt_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:id="@+id/bt_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2" /> <Button android:id="@+id/bt_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3" /> </LinearLayout>MainActivity文件:
public class MainActivity extends Activity { private Button btn_1, btn_2, btn_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_1 = (Button) findViewById(R.id.bt_1); btn_2 = (Button) findViewById(R.id.bt_2); btn_3 = (Button) findViewById(R.id.bt_3); btn_1.setOnClickListener(new MyListener()); btn_2.setOnClickListener(new MyListener()); btn_3.setOnClickListener(new MyListener()); } class MyListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_1: Toast.makeText(MainActivity.this, "这是第一个按钮", Toast.LENGTH_SHORT).show(); break; case R.id.bt_2: Toast.makeText(MainActivity.this, "这是第二个按钮", Toast.LENGTH_SHORT).show(); break; case R.id.bt_3: Toast.makeText(MainActivity.this, "这是第三个按钮", Toast.LENGTH_SHORT).show(); break; } } } }
相关文章推荐
- android listview适配器里区分不同按钮的点击事件
- android下 按钮点击 的四种写法
- Android 响应按钮点击事件的4种写法
- 2014-10-25Android学习--------按钮点击事件的写法
- android listview适配器里区分不同按钮的点击事件
- Android - Button(按钮)的响应点击事件的4种写法
- android_02_按钮点击事件的4种写法
- android listview适配器里区分不同按钮的点击事件
- Android中按钮的点击事件的四种写法
- Android_01_按钮点击事件的四种写法
- Android按钮点击事件的常用写法
- android listview适配器里区分不同按钮的点击事件
- Android按钮点击监听事件的写法
- Android中ListView适配器里区分不同按钮的点击事件
- Android按钮的点击事件的4种写法
- Android自定义按钮点击事件和长按事件的区分
- Android按钮点击事件的常用写法
- android 按钮两次点击事件区分
- Android Day01-Android按钮点击事件的4种写法
- Android - Button(按钮)的响应点击事件的4种写法