gpt4 book ai didi

android - 为什么我的 Toast 不显示,android 编程

转载 作者:行者123 更新时间:2023-11-29 19:18:36 26 4
gpt4 key购买 nike

我编写了代码,当单击特定的单选按钮时会显示特定消息的 toast,但是当我单击时为什么没有弹出 toast?为什么 toast 没有出现?我编写了代码,当单击特定的单选按钮时会显示特定消息的 toast,但是当我单击时为什么没有弹出 toast?为什么 toast 没有出现?

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>

最佳答案

您的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:
}
}

关于android - 为什么我的 Toast 不显示,android 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42905088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com