EditText does not accept digits as input

2019-04-28 17:58发布

问题:

The EditText view with id="price" has an inputType="numberDecimal" that does not accept any digits but will only accept one decimal point. The other EditText view with id="store" has no inputType set but has two issues. If the view has no input then entering a digit from the keyboard doesn't do anything. The only way to have a digit be accepted is if it immediately follows either a letter or another digit. I am using the default Android keyboard. The layout is being inflated in the onCreateView() method of a subclass of SherlockFragment. Could this be an issue with the EditText view behavior inside a SherlockFragment? I have a similar layout that is used to inflate a subclass of 'SherlockFragmentActivity', and those EditText views behave normally. Does anyone know what could be causing this issue?

<?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">
    <TextView 
        style="@style/ProductTextViewTitle"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:padding="10dp" 
        android:text="Product Tile" />
    <TableRow android:layout_width="fill_parent" 
              android:layout_height="50dp"     
              android:gravity="center_vertical"
              android:padding="5dp">
        <TextView 
              style="@style/ApolloStyleBold"
              android:layout_width="0dp" 
              android:layout_height="match_parent" 
              android:layout_weight="0.4"
              android:gravity="center_vertical" 
              android:paddingLeft="5dp" 
              android:text="@string/price"/>
        <EditText android:id="@+id/price" 
                  android:layout_width="0dp" 
                  android:layout_height="match_parent"
                  android:layout_weight="0.6" 
                  android:background="@null" 
                  android:hint="@string/price_italic_hint"
                  android:inputType="numberDecimal"/>
    </TableRow>
    <TableRow android:layout_width="fill_parent" 
              android:layout_height="50dp"    
              android:gravity="center_vertical"
              android:padding="5dp">
        <TextView   
            style="@style/ApolloStyleBold"
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="0.4"
            android:gravity="center_vertical" 
            android:paddingLeft="5dp" 
            android:text="@string/store_name"/>
        <EditText android:id="@+id/store" 
                  android:layout_width="0dp" 
                  android:layout_height="match_parent"
                  android:layout_weight="0.6" 
                  android:background="@null" 
                  android:hint="@string/stores_hint"
        />
    </TableRow>
</LinearLayout>

Update

When I tested this on Android v4.3, it has the messed up (not accepting digits as input) behavior. However, I tested it on Android v4.1.2, and it accepts digits as input. Is there something different about these two versions as far as EditText behavior?

回答1:

It turns out the issue wasn't with the android:inputType that was set, but with a ViewGroup higher up in the view hierarchy. The layout which is used to inflate the SherlockFragment was a child in a more complex layout.

A few parents up the view hierarchy there was a subclass of the FrameLayout ViewGroup called DragLayer. The DragLayer class implemented an Interface called DragController.DragListener which has a method dispatchKeyEvent(KeyEvent event). This method in DragLayer uses the @Override notation however did not call super.dispatchKeyEvent(event). After adding this line of code, then the event of clicking a digit on the keyboard did not get consumed and all works now.



回答2:

You can use following code in price edittext

android:inputType="number|numberDecimal"  // this means number or decimal


回答3:

Try this.

android:inputType="numberDecimal|numberSigned"

I think you should have a look here



回答4:

Try placing the code below on your EditText xml:

android:digits="0123456789"

This tells your EditText to accept only these digits. Of course you can modify it to whatever you need.



回答5:

Have you tried to set android:numeric="integer|signed|decimal"? Alternatively, you can set the KeyListener with setKeyListener() method, and force the EditText to accept the number/punct/character.



回答6:

Your code is working fine on my emulator. But there is a small thing which might be the answer to your problem, please input the digits from the number keys at the top of the character keys of your keyboard. Please do not use your numeric keyboard of the computers keyboard to enter the numbers. Hope it will help you.