使用Layout在AlertDialog构建器中检索EditTexts的值

时间:2010-07-20 23:27:06

标签: android alertdialog

使用以下代码段,我正在尝试获取已输入EditText s的文本值。

LayoutInflater factory = LayoutInflater.from(this);
final View loginView = factory.inflate(R.layout.login, null);

b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Dialog d = new AlertDialog.Builder(NewOrder.this)
            .setIcon(R.drawable.icon)
            .setTitle("Log In")
            .setView(loginView)
            .setPositiveButton("Log In", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    mSpinner = ProgressDialog.show(mContext, "", "Authenticating User...");
                    new LoginTask().execute();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    /* User clicked cancel so do some stuff */
                }
            })
            .create();
        d.show();
    }
});

我的 login.xml 非常标准且直截了当:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/username_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="Email Address"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/username"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/password_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="Password"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/password"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

当有人点击PositiveButton时,如何获取EditText字段的值?

2 个答案:

答案 0 :(得分:1)

.setPositiveButton("Log In", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        mSpinner =  ProgressDialog.show(mContext, "", "Authenticating     User...");

        /* == Get EditText Values Example == */
        EditText edtext_username=  (EditText) findViewById(R.id.username);
        EditText edtext_password=  (EditText) findViewById(R.id.password);

        String ValueofUsername = edtext_username.getText().toString(); 
        String ValueofPassword = edtext_password.getText().toString();
        /* ========== End Example ========== */

        new LoginTask().execute();
    }
})

答案 1 :(得分:0)

我不确定你可以像匿名课那样直接打电话给findViewById(),因为接受的答案表明了这一点。对我而言,它给出了 -

The method findViewById() is undefined for the type new DialogInterface.OnClickListener(){}

您必须拨打已经引用的View loginView。同样重要的是loginView被声明为最终在匿名类OnClickListener中引用它。

final View loginView = factory.inflate(R.layout.login, null);
// loginView should be declared final
b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Dialog d = new AlertDialog.Builder(NewOrder.this)
            .setIcon(R.drawable.icon)
            .setTitle("Log In")
            .setView(loginView)
            .setPositiveButton("Log In", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                   mSpinner = ProgressDialog.show(mContext, "", "Authenticating User...");
                   EditText usernameText=  (EditText) loginView.findViewById(R.id.username);
                   EditText passwordText=  (EditText) loginView.findViewById(R.id.password);
                   new LoginTask().execute();
                }
            })
            //more code