Android 警报对话框数字输入

发布于 2024-12-14 19:09:00 字数 3082 浏览 0 评论 0原文

好的,大家好,我有一个警报对话框,显示一个编辑文本,要求在活动中输入密码。一切正常我也尝试使用一个简单的布局文件。现在的问题是,每当我尝试强制执行数字输入时,它都会显示数字输入,但拒绝显示在日志中输入的任何数字输入,并显示空白字符串。它适用于常规文本。但是还有一个奇怪的问题,如果您输入的第一个字符是数字,那么它仍然存在相同的问题,但如果您输入一些非整数字符然后是数字,那么就可以了,例如。 a5 很好,但如果你输入 5,那么当你专门强制执行 android:inputType="number" 时就会出现问题 现在,当我在常规活动中尝试此操作(作为健全性检查)时,它工作正常,只是不在警报对话框中,

这是相关代码

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



    public void passwordChecker() {
        // TODO Auto-generated method stub
        //final EditText input = new EditText(PrivacyActivity.this);
        //input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        //input.setInputType(InputType.TYPE_CLASS_NUMBER);
        //input.setInputType(InputType.TY);
        //input.setTransformationMethod(PasswordTransformationMethod.getInstance());
        LayoutInflater factory = LayoutInflater.from(mContext);
        final View view = factory.inflate(R.layout.password, null);

        final EditText mPassword=(EditText) view.findViewById(R.id.password);

        //final EditText test=(EditText) findViewById(R.id.password);

        final EditText input = new EditText(mContext);

        if(true){
            new AlertDialog.Builder(PrivacyActivity.this).setMessage("Enter the password")
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    //perform deletion from database
                    dialog.cancel();

                    Log.e("error", "Apple "+ mPassword.getText().toString());


                }
            }).setView(input)
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    ....
                }
            }).create().show(); 
        }
    }

Ok guys so I have a alert dialog showing a Edittext asking for password in an activity. everything works fine i also tried using a simple layout file. now the problem is that whenever i try to enforce the numeric input it shows the numeric input but refuses to show any numeric input that is being typed in also in log there blank string show up. It works fine with regular text. But there is also a little weird problem with that if the first charater that u entered is number then it still has the same problem but if u enter some non integer character and then number then it is ok for eg. a5 is good but if u enter 5 then sam problem when u specifically enforce android:inputType="number"
Now when i try this in regular activity(as a sanity check) it works fine just not in the alertDialog

here is the relevance code

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



    public void passwordChecker() {
        // TODO Auto-generated method stub
        //final EditText input = new EditText(PrivacyActivity.this);
        //input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        //input.setInputType(InputType.TYPE_CLASS_NUMBER);
        //input.setInputType(InputType.TY);
        //input.setTransformationMethod(PasswordTransformationMethod.getInstance());
        LayoutInflater factory = LayoutInflater.from(mContext);
        final View view = factory.inflate(R.layout.password, null);

        final EditText mPassword=(EditText) view.findViewById(R.id.password);

        //final EditText test=(EditText) findViewById(R.id.password);

        final EditText input = new EditText(mContext);

        if(true){
            new AlertDialog.Builder(PrivacyActivity.this).setMessage("Enter the password")
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    //perform deletion from database
                    dialog.cancel();

                    Log.e("error", "Apple "+ mPassword.getText().toString());


                }
            }).setView(input)
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    ....
                }
            }).create().show(); 
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗笺 2024-12-21 19:09:00

试试这个...

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

Try this...

<EditText
        android:id="@+id/password"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:singleLine="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"        
        android:textAppearance="?android:attr/textAppearanceMedium"/>
阳光下的泡沫是彩色的 2024-12-21 19:09:00

所以我发现问题后花了很长时间才做出回应
问题不在于上面的任何一个。由于某种原因,我未能添加上面不存在的代码。

所以我用我的功能之一覆盖了警报对话框
如下。由于我只听KEYCODE_DPAD_UPKEYCODE_DPAD_CENTER
它弄乱了数字键盘输入,

.setNegativeButton("I don't Agree", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                }).setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                            KeyEvent event) {
                        if (keyCode < KeyEvent.KEYCODE_DPAD_UP || keyCode > KeyEvent.KEYCODE_DPAD_CENTER) {
                            return true;
                        }
                        return false;
                    }
                }).create().show();  

因此对于任何人来说,如果它有助于检查您正在听什么键。

So my bad took me ages to respond after i found the problem
The problem wasn't either of them above. For some reason I failed to add a code that isn't present above.

so i had over-ridded the alert dialog with one of my function
as follows. And since i was only listening to the KEYCODE_DPAD_UPandKEYCODE_DPAD_CENTER
it was messing up the numeric keyboard input

.setNegativeButton("I don't Agree", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                }).setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                            KeyEvent event) {
                        if (keyCode < KeyEvent.KEYCODE_DPAD_UP || keyCode > KeyEvent.KEYCODE_DPAD_CENTER) {
                            return true;
                        }
                        return false;
                    }
                }).create().show();  

so for anyone if it helps check what keys u r listening as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文