3

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

我有一个自定义类:

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

4

3 回答 3

0

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

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

我的自定义方法:

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);
于 2018-10-25T14:05:08.233 回答
-1

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

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

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

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

 return value;

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

final boolean value;

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

于 2012-09-04T13:18:24.490 回答