gpt4 book ai didi

java - 如何用自定义对话框列表替换默认的android对话框列表

转载 作者:行者123 更新时间:2023-12-02 04:16:56 24 4
gpt4 key购买 nike

我为我的对话框列表设计了一个自定义布局,它允许用户从列表中仅选择一个选项 code>,我想使用它而不是默认的 dialog。我真的不知道该怎么做。我有这个 fragment interface 如下所示;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;`


public class SingleChoiceClass extends DialogFragment {`

final CharSequence[] items = {"b1", "b2", "b3", "b4"};
String selection;
@NonNull
@Override


public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());`


builder.setTitle("Choose").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1){
case 0:
selection = (String) items[arg1];
break;
case 1:
selection = (String) items[arg1];
break;
case 2:
selection = (String) items[arg1];
break;
case 3:
selection = (String) items[arg1];
break;

}

}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {


// Display toast with the user's selection
Toast.makeText(getActivity(), "Your choice is : " + selection, Toast.LENGTH_SHORT).show();

}
});
return builder.create();
}
}

我的自定义布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="350dp"
android:layout_height="350dp"
android:background="@color/button_material_light"
android:layout_gravity="center">


<TextView
android:layout_width="350dp"
android:layout_height="50dp"
android:text="@string/textview"
android:layout_gravity="top"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="20dp"
android:textSize="30sp"
android:textAlignment="center"
android:textStyle="normal"
android:id="@+id/textview" />


<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">


<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/radio1"
android:checked="false"
android:id="@+id/radio1"
android:textSize="25sp"
android:background="#2bf308"
android:clickable="true" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:checked="false"
android:text="@string/radio2"
android:id="@+id/radio2"
android:textSize="25sp"
android:background="#f4fd02"
android:clickable="true" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/radio3"
android:checked="false"
android:id="@+id/radio3"
android:textSize="25sp"
android:background="#fb8e35"
android:clickable="true" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="@string/radio4"
android:checked="false"
android:id="@+id/radio4"
android:textSize="25sp"
android:background="#fc3434"
android:clickable="true" />
</RadioGroup>

<Button
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="@string/button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:clickable="true" />

</LinearLayout>

代码工作正常,但我需要使用自定义对话框而不是默认的。提前致谢。

最佳答案

请尝试一下

    //Please try this
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog dailog = new AlertDialog(getActivity());`

LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_layout, null);

dialog.setTitle("Choose").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
switch (arg1){
case 0:
selection = (String) items[arg1];
break;
case 1:
selection = (String) items[arg1];
break;
case 2:
selection = (String) items[arg1];
break;
case 3:
selection = (String) items[arg1];
break;

}

}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {


// Display toast with the user's selection
Toast.makeText(getActivity(), "Your choice is : " + selection, Toast.LENGTH_SHORT).show();

}
});
dialog.setView(view);
return view;
}

关于java - 如何用自定义对话框列表替换默认的android对话框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206363/

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