android - 带有 setcontentview 的警报对话框

标签 android button android-alertdialog customdialog

我想创建一个自定义警报对话框。所以我使用了以下代码。

public class MyAlertDialog extends AlertDialog {

    protected MyAlertDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void show() {
        // TODO Auto-generated method stub
        super.show();
        setContentView(R.layout.logindialog);
    }
}

logindialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layoutgrey" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="@dimen/mediuem"
        android:text="username" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:textSize="@dimen/mediuem"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="12dp"
        android:textSize="@dimen/mediuem"
        android:text= Password" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:textSize="@dimen/mediuem"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button2"
        android:background="@drawable/buttons"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:text="Cancel" />

    <Button
        android:id="@+id/button1"
        android:background="@drawable/buttons"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_below="@+id/editText2"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="28dp"
        android:layout_alignParentLeft="true"
        android:text="Ok" />

</RelativeLayout>

所以当我尝试像这样使用自定义创建的按钮时

final MyAlertDialog alert = new MyAlertDialog(Home.this);
alert.show();
Button ok=(Button)alert.findViewById(R.id.button1);
Button cancel=(Button)alert.findViewById(R.id.button2);

ok.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub
    }
});

它说空指针异常。 请帮忙弄清楚这里出了什么问题。

提前致谢

最佳答案

这是一个自定义的是-否对话框。它类似于您要实现的目标。您可以轻松地使其适应您的布局。

在构造函数中扩展您的自定义布局

通过 ID 找到您的 Button

设置 ButtonOnClickListener :在 onClick() 中调用一个您稍后需要实现的抽象方法。

public abstract class YesNoDialog extends Dialog 
{
    private String mMessage = "";
    private TextView mText;
    private Button mYes;
    private Button mNo;

    public YesNoDialog(Context context, String message)
    {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View v = getLayoutInflater().inflate(R.layout.yes_no_dialog, null);
        mText = (TextView) v.findViewById(R.id.message);
        mMessage = message;
        mText.setText(mMessage);
        mYes = (Button) v.findViewById(R.id.yes);
        mYes.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                dismiss();
                onYes();
            }
        });
        mNo = (Button) v.findViewById(R.id.no);
        mNo.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                dismiss();
                onNo();
            }
        });
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(v);
    }

    public abstract void onYes();

    public abstract void onNo();

}


这是如何使用它:

YesNoDialog dialog = new YesNoDialog(this, "message")
{

    @Override
    public void onYes()
    {
      // do something           
    }

    @Override
    public void onNo()
    {
      // do something
    }
};
dialog.show();

关于android - 带有 setcontentview 的警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15362776/

相关文章:

java - 向 Java Applet 添加按钮

arrays - 如何让两个变量相互依赖

android - android 2.1 中的 AlertDialog

java - 创建 AlertDialog 时代码中断。我想我有上下文错误......?

android - AlertDialog 不会在我的应用程序中显示 - 为什么会这样?

android - 用gradle从2个依赖 jar 创建一个jar

android - 设置 AutoCompleteTextView 分隔线高度在 android 中无效

objective-c - 右侧标注附件按钮在此代码的注释 View 上不可见

android - 带图片和文字的ListView

android - 条件链单一且可完成