gpt4 book ai didi

android - 自定义类中的自定义对话框返回 bool 值

转载 作者:太空狗 更新时间:2023-10-29 15:27:28 25 4
gpt4 key购买 nike

我已经搜索了答案但没有找到,所以请帮助我:)

我有一个自定义类:

public class CustomClass {

private final Context ctx;
public CustomClass(Context ctx) {
this.ctx = ctx;
}

public boolean setDialog(int head, int text) {
final boolean value;

final Dialog d = new Dialog(ctx);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.custom2_dialog);

TextView txtHead = (TextView) d.findViewById(R.id.custom2_txtHead);
txtHead.setText(ctx.getResources().getString(head));
TextView txtText = (TextView) d.findViewById(R.id.custom2_txtText);
txtText.setText(ctx.getResources().getString(text));

Button btnOK = (Button) d.findViewById(R.id.custom2_btnOK);
btnOK.setText("OK");
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
value = true;
d.dismiss();
}
});

Button btnNO = (Button) d.findViewById(R.id.custom2_btnNO);
btnNO.setText("NO");
btnNO.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
value = false;
d.dismiss();
}
});

d.show();

return value;
}
}

您可以看到我在自定义类中创建了一个自定义对话框,因为我不想在每个 Activity 中都创建一个对话框。现在,当我在 Activity 中使用它时:

CustomClass cC = new CustomClass(this);
if(cC.setDialog(R.string.head, R.string.text)) {
// user checked OK
} else {
// user checked NO
}

如何知道用户是否选中了 OK 或 NO,因为返回 true、false 值在自定义类中不起作用,对话框不会在用户单击之前等待,它会自动返回一个值。

最佳答案

这是您问题的解决方案。首先,如果您正在创建自定义对话框,那么您必须扩展 Dialog 类以读取响应并实现 OnClickListener

class CustomizeDialog extends Dialog implements OnClickListener
{
// some code
public CustomizeDialog(Activity activity, String title, String msg, int i, Typeface typeface)
{
super(activity);
this.activity = activity;
intFlag = i;
setContentView(R.layout.relative);
dialogButtonYes = (Button) findViewById(R.id.buttonCustomDialogYes);
dialogButtonNo = (Button) findViewById(R.id.buttonCustomDialogNo);
textView = (TextView) findViewById(R.id.textViewCustomTitle);
this.msg = msg;
dialogButtonNo.setOnClickListener(this);
dialogButtonYes.setOnClickListener(this);
}
}

@Override
public void onClick(View v) {
if (v == dialogButtonYes)
{
Intent intent =new Intent(activity, CallForOne.class);
activity.startActivity(intent);
dismiss();
}
else
dismiss();
}
else if(intFlag == 2)
{
if (v == dialogButtonYes)
{
Intent intent =new Intent(activity, CallMe.class);
activity.startActivity(intent);
dismiss();
}
else
dismiss();
}

relative.xml文件

<TextView
android:id="@+id/textViewCustomTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewCustomTitle"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >

<Button
android:id="@+id/buttonCustomDialogYes"
style="@style/styleNormalButton"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="@string/yes" />

<Button
android:id="@+id/buttonCustomDialogNo"
style="@style/styleNormalButton"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="@string/no" />
</LinearLayout>

和主类

    class Main implements OnClickListener
{
onCreate()
{
//your code
}

public void on click()
{
customizeDialog = new CustomizeDialog(CustomDialogExample.this,getString(R.string.title_for_one),getString(R.string.msg_for_one),1,"String");
customizeDialog.show();
}
}

关于android - 自定义类中的自定义对话框返回 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263532/

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