我编写了代码,当单击特定的单选按钮时,会显示某些消息的吐司,但是当我单击时,为什么不会弹出吐司?
为什么吐司没有出现?
我编写了代码,当单击特定的单选按钮时,会显示某些消息的吐司,但是当我单击时,为什么不会弹出吐司?
为什么吐司没有出现?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
int id;
String text = "radio button 1!";
String text2="radioi button 2";
String text3="radio button 3";
int duration = Toast.LENGTH_SHORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
id = radioGroup.getCheckedRadioButtonId();
}
public void onRadioButtonClicked(View view) {
switch(id) {
case R.id.radioButton:
Toast toast = Toast.makeText(this, text, duration);
toast.show();
break;
case R.id.radioButton2:
Toast toast2 = Toast.makeText(this, text2, duration);
toast2.show();
break;
case R.id.radioButton3:
Toast toast3 = Toast.makeText(this, text3, duration);
toast3.show();
default:
}
}
}
这是xml布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.ai_agamcompaq.radio.MainActivity">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_group">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="115dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2"
android:layout_below="@+id/radioButton"
android:layout_alignStart="@+id/radioButton"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="28dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton3"
android:layout_below="@+id/radioButton2"
android:layout_alignStart="@+id/radioButton2"
android:onClick="onRadioButtonClicked"
android:layout_marginTop="52dp" />
</RadioGroup>
</RelativeLayout>
最佳答案
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
// Declare the messages to show
private String text1 = "radio button 1";
private String text2 = "radio button 2";
private String text3 = "radio button 3";
private int mToastDuarion = Toast.LENGTH_SHORT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id. radio_group);
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int id) {
switch(id) {
case R.id.radioButton:
Toast.makeText(this, text1, mToastDuarion).show();
break;
case R.id.radioButton2:
Toast.makeText(this, text2, mToastDuarion).show();
break;
case R.id.radioButton3:
Toast.makeText(this, text3, mToastDuarion).show();
break;
default:
break;
}
}
}
layout_main.xml
(这是未定义onClick
的相同XML):<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:layout_width="368dp"
android:layout_height="wrap_content"
android:id="@+id/radio_group"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="115dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2"
android:layout_below="@+id/radioButton"
android:layout_alignStart="@+id/radioButton"
android:layout_marginTop="28dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton3"
android:layout_below="@+id/radioButton2"
android:layout_alignStart="@+id/radioButton2"
android:layout_marginTop="52dp" />
</RadioGroup>
</LinearLayout>