1

我已经实现了一个注册活动,其中包含 viewpager 中的片段。在其中一个片段中,我有一个表单,其中包含一些动态创建的 EditText。问题是,如果我将输入类型数字或电话设置为此 EditText 之一,那么键盘将停止输入数字。如果我将输入类型设置为文本,那么它会按预期工作。

这是我设置输入类型的代码:

switch(mDatosSocio.getTipo()){
                case "text":
                    etFormField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
                    break;
                case "number":
                    etFormField.setInputType(InputType.TYPE_CLASS_PHONE);
                    break;
                case "email":
                    etFormField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                    break;
                default:
                    break;
            }

有什么想法吗?提前致谢!

编辑 解决方案

问题是我在父活动中有这个功能,所以它阻止了数字的输入。

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction()==KeyEvent.ACTION_DOWN) {
        Log.d("KEYDOWN", event.getKeyCode()+"");
    }
    else if (event.getAction()==KeyEvent.ACTION_UP) {
        Log.d("KEYUP", event.getKeyCode()+"");
    }
    Log.d("KEY", event.getKeyCode()+"");
    return true;
}
4

2 回答 2

1

像这样添加并检查

EditText etFormField= new EditText(context);
etFormField.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); //for decimal numbers
etFormField.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED); //for positive or negative values

在你的情况下可能像这样

etFormField.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

或者像这样你也可以尝试特定类型的输入类型值

etFormField.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_PHONE );
于 2017-11-09T07:54:30.580 回答
1

应用附加选项 TYPE_NUMBER_FLAG_DECIMAL。

etFormField.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL)



编辑:

这是我的示例代码,并在 Galaxy S7 中使用参考键盘应用程序进行了测试。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button button1;
    Button button2;

    EditText editText1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.btn_1);
        button2 = findViewById(R.id.btn_2);
        editText1 = findViewById(R.id.real_edittext);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            }
        });
    }
}

activity_main.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:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"/>

        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"/>

    </LinearLayout>


    <EditText
        android:id="@+id/real_edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

</LinearLayout>
于 2017-11-09T07:51:03.417 回答