简体   繁体   English

Android - editText 中的默认值

[英]Android - default value in editText

In my app I have an edit user details page and I want to display the current name, email address etc in the corresponding editText fields and then the user can just erase that and enter a new one if they want.在我的应用程序中,我有一个编辑用户详细信息页面,我想在相应的 editText 字段中显示当前姓名、电子邮件地址等,然后用户可以删除它并根据需要输入一个新的。

Is there a way to do this?有没有办法做到这一点? Thanks for any help谢谢你的帮助

There is the hint feature?hint功能吗? You can use the setHint() to set it, or set it in XML (though you probably don't want that, because the XML doesn't 'know' the name/adress of your user :) )您可以使用setHint()来设置它,或在 XML 中设置它(尽管您可能不想要那样,因为 XML 不“知道”您的用户的姓名/地址:))

You can use EditText.setText(...) to set the current text of an EditText field.您可以使用EditText.setText(...)来设置 EditText 字段的当前文本。

Example:例子:

yourEditText.setText(currentUserName);

从xml:

  android:text="yourtext"

You can use text property in your xml file for particular Edittext fields.您可以在xml文件中为特定的 Edittext 字段使用text属性。 For example :例如 :

<EditText
    android:id="@+id/ET_User"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="yourusername"/>

like this all Edittext fields contains text whatever u want,if user wants to change particular Edittext field he remove older text and enter his new text.像这样,所有 Edittext 字段都包含您想要的文本,如果用户想要更改特定的 Edittext 字段,他会删除旧文本并输入他的新文本。

In Another way just you get the particular Edittext field id in activity class and set text to that one.另一种方式中,您只需在活动类中获取特定的Edittext 字段 id并将文本设置为该字段

Another way = programmatically另一种方式=以编程方式

Example:例子:

EditText username=(EditText)findViewById(R.id.ET_User);

username.setText("jack");

First you need to load the user details somehow首先,您需要以某种方式加载用户详细信息

Then you need to find your EditText if you don't have it-然后你需要找到你的 EditText 如果你没有它 -

EditText et = (EditText)findViewById(R.id.youredittext);

after you've found your EditText, call找到 EditText 后,请致电

et.setText(theUserName);

You can do it in this way你可以这样做

private EditText nameEdit;
private EditText emailEdit;
private String nameDefaultValue = "Your Name";
private String emailDefaultValue = "abc@xyz.com";

and inside onCreate method在 onCreate 方法中

nameEdit = (EditText) findViewById(R.id.name);
    nameEdit.setText(nameDefaultValue); 
    nameEdit.setOnTouchListener( new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (nameEdit.getText().toString().equals(nameDefaultValue)){
                nameEdit.setText("");
            }
            return false;
        }
    });

    nameEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {               
            if(!hasFocus && TextUtils.isEmpty(nameEdit.getText().toString())){
                nameEdit.setText(nameDefaultValue);
            } else if (hasFocus && nameEdit.getText().toString().equals(nameDefaultValue)){
                nameEdit.setText("");
            }
        }
    });     

    emailEdit = (EditText)findViewById(R.id.email);
    emailEdit.setText(emailDefaultValue);   
    emailEdit.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {               
            if(!hasFocus && TextUtils.isEmpty(emailEdit.getText().toString())){
                emailEdit.setText(emailDefaultValue);
            } else if (hasFocus && emailEdit.getText().toString().equals(emailDefaultValue)){
                emailEdit.setText("");
            }
        }
    });
 public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText et = (EditText) findViewById(R.id.et);
        EditText et_city = (EditText) findViewById(R.id.et_city);
        // Set the default text of second EditText widget
        et_city.setText("USA");
 }
}

使用 android android:hint 设置默认值或 android:text

We wish there is a default value attribute in each view of android views or group view in future versions of SDK.我们希望在未来版本的 SDK 中,android 视图或组视图的每个视图都有一个默认值属性。 but to overcome that, simply before submission, check if the view is empty equal true, then assign a default value但是为了克服这个问题,在提交之前,检查视图是否为空等于真,然后分配一个默认值

example:例子:

   /* add 0 as default numeric value to a price field when skipped by a user,
                    in order to avoid parsing error of empty or improper format value. */
                    if (Objects.requireNonNull(edPrice.getText()).toString().trim().isEmpty())
                    edPrice.setText("0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM