我写了代码,当点击特定单选按钮显示某些消息的吐司时,但是当我点击为什么没有吐司弹出? 为什么吐司没有露面? 我写了代码,当点击特定的单选按钮显示某些消息的吐司,但当我点击为什么没有吐司弹出? 为什么吐司没有出现?
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>
答案 0 :(得分:2)
您的switch
声明不正确。你永远不会看到一个复选框,因为你要求被检查的人。
public void onRadioButtonClicked(View view) {
switch(view.getId()) {
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:
}
}
答案 1 :(得分:0)
写一行代码......
Toast.makeText(上下文,&#34;您的文字&#34;,Toast.LENGTH_SHORT).show();
答案 2 :(得分:0)
public void onRadioButtonClicked(View view) {
//please update the ID
id = view.getId();
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:
}
}
您没有更新ID,因此切换语句始终是默认的。
答案 3 :(得分:0)
MainActivity.java
:
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>
答案 4 :(得分:0)
作为JonZarate答案的替代方案,你也可以试试这个:
-(void)keyDown:(NSEvent *)theEvent
{
NSLog(@"MyTextView keyDown: %@", theEvent.characters);
static bool b = true;
if (b) { // this silly example only lets every other keypress through.
[super keyDown:theEvent];
}
b = !b;
}