gpt4 book ai didi

java - 如何使用 AlertDialog 预选单选按钮?

转载 作者:搜寻专家 更新时间:2023-11-01 08:10:38 25 4
gpt4 key购买 nike

有人可以帮我吗?我有一个 AlertDialog 框,它工作正常,但我想要的是当弹出 alertdialog 时,它会自动选中其中一个单选按钮。

例如弹出提示对话框时勾选“10分钟”。

怎么做?

下面是我的工作代码。

final CharSequence[] deleteFilesBy = {"5 minutes","10 minutes", "15 minutes"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete Files by?")
.setSingleChoiceItems(deleteFilesBy,-1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if(deleteFilesBy[which]=="5 minutes")
{
// do something
}
else if (deleteFilesBy[which]=="10 minutes")
{
// do something
}
else if (deleteFilesBy[which]=="15 minutes")
{
// do something
}
dialog.dismiss();
}
}).setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.dismiss();
}
});

AlertDialog alert = builder.create();
alert.show();

请帮忙!

谢谢

RJUY

最佳答案

TestRadio.java:

package com.dave.kelley;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class TestRadioActivity extends Activity {

int timeSetting = 0;
RadioButton radio0;
RadioButton radio1;
RadioButton radio2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(listener);
radio0 = (RadioButton) findViewById(R.id.radio0);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
}

public OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
if (timeSetting == 0) {
radio0.setChecked(true);
radio1.setChecked(false);radio2.setChecked(false);
}
if (timeSetting == 1) {
radio1.setChecked(true);
radio0.setChecked(false);radio2.setChecked(false);
}
if (timeSetting == 2) {
radio2.setChecked(true);
radio0.setChecked(false);radio1.setChecked(false);
}
timeSetting++;
if(timeSetting == 3 || timeSetting > 3) {
timeSetting = 0;
}
}
};
}

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />

<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />

</RadioGroup>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>

关于java - 如何使用 AlertDialog 预选单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9969501/

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