AlertDialogBox没有在android中输入任何值

时间:2017-07-22 03:29:22

标签: android android-edittext android-alertdialog

在我的AlertDialog框中,我使用的是EditText,但当我在EditText中输入任何值时,我都没有获得任何价值。

我已将setcancelebletouchmode用于true,但我在EditText中没有获得任何值。

我已将inputtype设置为数字,但是当我输入任何数字时,我没有获得任何值或EditText中显示任何值。

这是我的代码:

LayoutInflater li = LayoutInflater.from(this);
//Creating a view to get the dialog box
View confirmDialog = li.inflate(R.layout.layout_forlogin, null);

//Initizliaing confirm button fo dialog box and edittext of dialog box
btnconfirmmobile = (Button) confirmDialog.findViewById(R.id.btnlogin);
Mobile = (EditText) confirmDialog.findViewById(R.id.etlogin);

//Creating an alertdialog builder
final AlertDialog.Builder alert = new AlertDialog.Builder(this);

//Adding our dialog box to the view of alert dialog
alert.setView(confirmDialog);

//Creating an alert dialog
final AlertDialog alertDialog = alert.create();

//Displaying the alert dialog
alertDialog.show();


//On the click of the confirm button from alert dialog
btnconfirmmobile.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Hiding the alert dialog

        mobileconfirm = Mobile.getText().toString();

        if (mobileconfirm.equals("")) {
            Mobile.setError("Mobile no can not be blank");

        } else {

            new PostAsyncslogin().execute(mobileconfirm);
            //Getting the user entered otp from edittext
        }

    }
});
alertDialog.setOnKeyListener(new Dialog.OnKeyListener() {

    @Override
    public boolean onKey(DialogInterface arg0, int keyCode,
    KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            alertDialog.dismiss();
        }
        return true;
    }
});

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:orientation="vertical"

    android:padding="@dimen/activity_horizontal_margin">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/bgdialog"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:text="Enter Your Mobile"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000" />

        <EditText
            android:id="@+id/etlogin"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv"
            android:layout_centerHorizontal="true"
            android:layout_margin="10dp"
            android:inputType="number"
            android:maxLength="10">

            <requestFocus />
        </EditText>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnlogin"
                style="@style/Widget.AppCompat.Button.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="8"
                android:text=" Login "
                android:textColor="#000" />

            <Button
                android:id="@+id/btncancel"
                style="@style/Widget.AppCompat.Button.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:ems="8"
                android:text=" Cancel "
                android:textColor="#000" />

        </LinearLayout>

    </LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

我测试了你的代码并且它运行了。我认为问题不在这里。您可以记录mobileConfirm以在else语句中查看其值。只需输入 在else语句中的Log.e(“mobileconfirm”,mobileconfirm)

答案 1 :(得分:0)

在您的密钥监听器上从true更改为false 。如果侦听器已经使用了事件,则应该返回true,否则返回false。只需返回false,让框架为您处理其余部分。有关详细信息,请参阅Documentation

答案 2 :(得分:0)

尝试使用TextInputLayout显示错误并从编辑测试框中获取文本。例如:

<android.support.design.widget.TextInputLayout
android:id="@+id/tillogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <EditText
        android:id="@+id/etlogin"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:inputType="number"
        android:maxLength="10">
        </android.support.design.widget.TextInputLayout>

您的java代码:

           LayoutInflater li = LayoutInflater.from(this);
            //Creating a view to get the dialog box
            View confirmDialog = li.inflate(R.layout.layout_forlogin, null);

            //Initizliaing confirm button fo dialog box and edittext of dialog box
            btnconfirmmobile = (Button) confirmDialog.findViewById(R.id.btnlogin);
          EditText  mobileno = (EditText) confirmDialog.findViewById(R.id.etlogin);
          TextInputLayout  mobilenoError = (TextInputLayout) confirmDialog.findViewById(R.id.tillogin);

            //Creating an alertdialog builder
            final AlertDialog.Builder alert = new AlertDialog.Builder(this);

            //Adding our dialog box to the view of alert dialog
            alert.setView(confirmDialog);

            //Creating an alert dialog
            final AlertDialog alertDialog = alert.create();

            //Displaying the alert dialog
            alertDialog.show();


            //On the click of the confirm button from alert dialog
            btnconfirmmobile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Hiding the alert dialog

                    mobileconfirm = mobileno.getText().toString();

                    if (mobileconfirm.equals("")) {
                        mobilenoError.setError("Mobile no can not be blank");

                    } else {

                        new PostAsyncslogin().execute(mobileconfirm);
                        //Getting the user entered otp from edittext
                    }

                }
            });
            alertDialog.setOnKeyListener(new Dialog.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface arg0, int keyCode,
                                     KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        finish();
                        alertDialog.dismiss();
                    }
                    return true;
                }
            });

我希望这可以帮到你,一切顺利。