如何从android中的文本多次输入?

时间:2017-08-25 10:38:25

标签: android textview

我们如何在Android中使用TextView嵌入输入文本?

例如,我想实现这个:

“你的名字是___,这是等级____”

“___”是用户必须填写指定输入的空白​​区域,我会检查它以进行验证。

请注意,它与android:hint不同。

我该如何实现?

2 个答案:

答案 0 :(得分:0)

您可以尝试这样

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


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textSize="16sp"
        android:textColor="#000000"
        android:text="Your Name is"
        android:id="@+id/textview1" />

    <EditText
        android:layout_width="60dp"
        android:textSize="16sp"
        android:layout_height="40dp"
        android:textColor="#000000"
        android:id="@+id/edittext1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:textSize="16sp"
        android:textColor="#000000"
        android:text=", This is the Level"
        android:id="@+id/textview2" />

    <EditText
        android:layout_width="60dp"
        android:textSize="16sp"
        android:layout_height="40dp"
        android:textColor="#000000"
        android:id="@+id/edittext2"/>

</LinearLayout>

答案 1 :(得分:0)

您可以使用EditText中的常量文本使填充空白类型格式。通过在XML中创建EditText来完成此任务。

<EditText
    android:id="@+id/editText"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

在Java类中添加以下代码:

final EditText edittext = (EditText) findViewById(R.id.editText);
    edittext.setText("Something ");
    Selection.setSelection(edittext.getText(), edittext.getText().length());
    edittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) { if(!s.toString().contains("Something ")){
                edittext.setText("Something ");
                Selection.setSelection(edittext.getText(), edittext.getText().length());

            }

        }
    });

这是代码的输出。这里的字符串 Something 是静态的,无法删除。

enter image description here

您可以使用android:background="@android:color/transparent"隐藏Edittext中的下划线。我还建议您使用代码禁用Enter在EditText中:

<EditText
    android:id="@+id/editText"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:maxLines="1"      <----- add this 
    android:inputType="text"/>    <------ and this to disable enter in edittext