Android - 用于EditText字段的键盘从数字切换到全键盘

时间:2017-01-31 10:43:22

标签: android android-edittext

目前,我的活动有一个带有数字的EditText字段。点击字段进行编辑时,数字键盘会按预期显示。但是,当我按下主页按钮然后再次调出应用程序时,键盘会切换到全尺寸键盘,我无法编辑文本。点击EditText字段会短暂显示数字键盘,然后显示完整大小的键盘。有什么想法吗?

public void CreatePlaylistView()
{
    ArrayAdapter<FlowTrackSlot> ftsAdapter =
            new ArrayAdapter<FlowTrackSlot>(this, 0, playlistSlots) {


                @Override
                public View getView(final int position,
                                    View convertView,
                                    ViewGroup parent) {

                    final FlowTrackSlot currentSlot = playlistSlots.get(position);

                    ViewHolder viewHolder = null;
                    if(convertView == null) {
                        convertView = getLayoutInflater()
                                .inflate(R.layout.item, null, false);

                        viewHolder = new ViewHolder();
                        viewHolder.slotTrackName =
                                (TextView)convertView.findViewById(R.id.slotTrackName );
                        viewHolder.slotTrackWeight =
                                (EditText)convertView.findViewById(R.id.slotTrackWeight );

                        convertView.setTag(viewHolder);


                        viewHolder.slotTrackName.setText(currentSlot.playlistTrack.track.name);

                        final ViewHolder finalViewHolder = viewHolder;
                        viewHolder.slotTrackWeight.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                            }

                            @Override
                            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                            }

                            @Override
                            public void afterTextChanged(Editable editable) {

                                try {
                                    if(currentSlot.weight != Integer.parseInt(editable.toString()))
                                    {
                                        currentSlot.weight = Integer.parseInt(editable.toString());
                                        finalViewHolder.slotTrackWeight.setText(editable.toString());

                                    }


                                } catch (NumberFormatException e) {
                                    if(currentSlot.weight != 0)
                                    {
                                        currentSlot.weight = 0;
                                        finalViewHolder.slotTrackWeight.setText("0");

                                    }
                                }
                            }
                        });
                    }
                    else
                    {
                        viewHolder = (ViewHolder)convertView.getTag();
                    }

                    return convertView;

                }
            };

    ListView viewTrackPlaylist = new ListView(this);

    setContentView(viewTrackPlaylist);
    viewTrackPlaylist.setAdapter(ftsAdapter);
}

编辑:添加布局 activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>
</RelativeLayout>

item.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="horizontal"
    android:focusableInTouchMode="true">

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/slotTrackName"
    android:layout_weight="0.41"
    android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
    android:textSize="36sp"
    android:lineSpacingExtra="10sp"
    android:fontFamily="sans-serif" />

<EditText
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/slotTrackWeight"
    android:text="100"
    android:textSize="24sp"
    android:singleLine="true"
    android:selectAllOnFocus="true" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

发现这个有用的实现,显示了更好的键盘并限制了输入字符。

<EditText
  android:inputType="phone"
  android:digits="1234567890"
/>

您可以通过以下方式进行编程:

yourEditText.setInputType(InputType.TYPE_CLASS_PHONE);

KeyListener keyListener =  DigitsKeyListener.getInstance("1234567890");
yourEditText.setKeyListener(keyListener);