EditText具有默认值


trpnest
2025-03-28 10:14:55 (10天前)
  1. 我使用带有.input-group-addonbootstrap将元素前置或附加到单个表单元素,如TextArea输入类型的表单。

前置简单意味着它显示它包含禁止用户编辑的值。

我怎么能在我的身上做到这一点

安卓
</跨度>
应用程序吗?它与我的XML或什么?
请帮忙…
锟斤拷

3 条回复
  1. 0# Hey ou | 2019-08-31 10-32



    我认为以下代码可以帮助您这样做。




    解决方案1
    </强>




    TextView

    将只显示前缀文本,和

    EditText

    可以是用户的选择。




    1. @android:color/white" >

    2. <TextView
    3.     android:id="@+id/txtTitle"
    4.     android:layout_width="wrap_content"
    5.     android:layout_height="wrap_content"
    6.     android:text="http://"
    7.     android:textColor="@android:color/darker_gray" />
    8. <EditText 
    9.     android:layout_width="match_parent"
    10.     android:layout_height="wrap_content"
    11.     android:background="@android:color/white"
    12.     android:text="www.google.com"
    13.     />
    14. </code>


    在上面的布局中,您将看到

    TextView

    拥有默认文本,但用户只能写入

    EditText

    。所以用户不会对这种技巧有所了解。




    解决方案2
    </强>




    1. final EditText edt = (EditText) findViewById(R.id.editText1);

    2. edt.setText(“http://“);
      Selection.setSelection(edt.getText(), edt.getText().length());

    3. edt.addTextChangedListener(new TextWatcher() {

    4. @Override
    5. public void onTextChanged(CharSequence s, int start, int before, int count) {
    6.     // TODO Auto-generated method stub
    7. }
    8. @Override
    9. public void beforeTextChanged(CharSequence s, int start, int count,
    10.         int after) {
    11.     // TODO Auto-generated method stub
    12. }
    13. @Override
    14. public void afterTextChanged(Editable s) {
    15.     if(!s.toString().contains("http://")){
    16.         edt.setText("http://");
    17.         Selection.setSelection(edt.getText(), edt.getText().length());
    18.     }
    19. }
    20. });

    21. </code>


    此解决方案不允许用户从其中删除“http://”

    EditText

    输入。


  2. 1# 你咖喱人biss嗷 | 2019-08-31 10-32




    EditText

    扩展

    TextView

    延伸

    View

    类。和

    View.java

    class有方法

    setText

    这几乎适用于每个延伸的视图

    View.java





    Conclustion
    </强>




    1. EditText.setText(“your Text here”); //set default value

    2. EditText.setHint(“your Text here”); //set hint

    3. </code>



    在Xml中
    </强>




    1. android:text=”@string/yourStringText

    2. </code>

登录 后才能参与评论