【问题标题】:Custom dialog in custom class returns boolean value自定义类中的自定义对话框返回布尔值
【发布时间】:2012-08-29 02:47:01
【问题描述】:

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

我有一个自定义类:

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 中使用它时:

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 值在自定义类中不起作用,对话框不会在用户点击之前等待,它会自动返回一个值。

【问题讨论】:

  • Simple Buddy,而不是为对话框创建自定义类使用Application 类定义相同的方法,但方法setDialog()返回Void并全局声明变量boolean value,现在你可以获取用户选择的任何地方。
  • Application 类最好的一点是你不需要在每次显示 Dialog 时都传递当前 Activity 的上下文。
  • 无法同步获取返回值。您将不得不使用监听器
  • @user370305 我不知道你是什么意思 return void,当我将方法从 boolean 更改为 void 时,我不能返回任何东西,你有一个例子吗?
  • 你不需要返回值。仔细阅读我的第一条评论..

标签: android class dialog


【解决方案1】:

这是您问题的解决方案。 首先,如果您正在创建自定义对话框,那么您必须扩展 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();
    }
}

【讨论】:

  • 还有其他解决方案吗?我不想为此创建另一个类。
【解决方案2】:

我在这里遇到了同样的问题,然后我找到了一种解决方案,通过在 自定义对话框 中单击按钮打开 Activity。但是您也可以将此逻辑用于其他操作。

我的自定义方法:

public class Extension extends Activity{

  public void showDialogConfirma(Activity localActivity, Class destActivity, String titulo, String mensagem){

    final Dialog dialog = new Dialog(localActivity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_um_btn);

    TextView textTitulo = dialog.findViewById(R.id.txtTitle);
    textTitulo.setText(titulo);

    Button btnOk = dialog.findViewById(R.id.btnOk);
    btnOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            // here I can open any activity from any class
            localActivity.startActivity(new Intent(localActivity, destActivity));
        }
    });

    dialog.show();

  }
}

你这样调用这个方法:

Extension extension = new Extension();
// here you gonna pass your parameters to your Dialog on create it
// on my case the activity that I want open
extension.showDialogConfirma(ThisActivity.this, DestActivity.class, titulo, mensagem);

【讨论】:

    【解决方案3】:

    首先来自@user370305 的评论是正确的方法。毫无疑问,还有其他方法。

    您面临的问题是因为当您调用自定义类的方法时

       if(cC.setDialog(R.string.head, R.string.text));
    

    即使没有点击任何内容,您也会获得价值

     return value;
    

    as false 这是你声明时设置的默认值

    final boolean value;
    

    相反,您应该听“取消”和“确定”点击

    【讨论】:

    • 你能举个例子吗?我认为有一个简单的解决方案。
    • 那不是答案,他知道他的代码错了,你不需要再告诉他。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    相关资源
    最近更新 更多