您的位置:首页 > 移动开发 > Android开发

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 Button switch