Retrieving value of EditTexts in AlertDialog builder using Layout

孤街醉人 提交于 2019-12-21 05:02:13

问题


Using the following code snippets, I'm trying to get the text value which has been typed in to the EditTexts.

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();
    }
});

My login.xml is pretty standard and straight forward:

<?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>

When someone clicks the PositiveButton, how do I get the value of the EditText fields?


回答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();
    }
})



回答2:


I am not sure you can directly call findViewById() like that in the anonymous class as the accepted answer suggests. For me it gives -

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

You have to call on the View loginView that you already have referenced to. Also it is important loginView be declared as final to reference it in anonymous class 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


来源:https://stackoverflow.com/questions/3295200/retrieving-value-of-edittexts-in-alertdialog-builder-using-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!