一个更好的方式来为的OnClick领域的EditText? [英] A better way to OnClick for EditText fields?

查看:208
本文介绍了一个更好的方式来为的OnClick领域的EditText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText场,假设用户已经输入的文字到它。然后,用户想回来重新编辑文字:我想这个的EditText领域的特点是,如果你选择它,它已经在它的文字之后,它清除文本你之前,你可以输入一些新的<。 / p>

我尝试使用的EditText字段的OnClick方法,但是这要求我选择的EditText字段,然后点击它第二次,未尝不是显而易见的任何人,但我。我怎样才能尽快获取文本要清除的EditText字段作为用户选择呢?


解决方案

一般

您可以实现你想要通过的onfocus 和清除文本字段,类似于根据您的文章两评议人已经建议的组合做什么。一个解决方案是这样的:

 的EditText myEditText =(EditText上)findViewById(R.id.myEditText);
myEditText.setOnFocusChangeListener(新OnFocusChangeListener(){    @覆盖
    公共无效onFocusChange(视图V,布尔hasFocus){
        如果(hasFocus){
        //清除一个TextView到prevent机器人时,始终使用TextKeyListener
        //在日志中的警告
        TextKeyListener.clear((myEditText).getText());        }
    }
});

始终使用TextKeyListener为干净的EditText,就可以避免很多的Andr​​oid警告在日志中这样的。

但是......

我宁愿建议您只需设置在你的XML如下:

 &LT; EditText上的android:selectAllOnFocus =真/&GT;

就像描述<一个href=\"http://stackoverflow.com/questions/4669464/select-all-text-inside-edittext-when-it-gets-focus\">here.这样,你的用户有一个更好的用户界面,感觉它,他或她可以在他/她自己决定做什么的文字并不会生气,因为它每次都清除出去!

I have an EditText field, suppose the user has already entered text into it. Then the user wants to come back to edit the text again: the feature I want for this EditText field is that if you select it after it already has text in it, it clears the text for you before you can type something new in.

I tried using the EditText field's OnClick method, but this required that I select the EditText field, then click on it a second time, something that isn't obvious to anyone but me. How can I get the text to clear from the EditText field as soon as the user selects it?

解决方案

In General

You can achieve what you want to do via a combination of onFocus and clearing the text field, similar to what the two commenters under your post already suggested. A solution would look like this:

EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
        // Always use a TextKeyListener when clearing a TextView to prevent android
        // warnings in the log
        TextKeyListener.clear((myEditText).getText());

        }
    }
});

Please always use a TextKeyListener to "clean" EditText, you can avoid a lot of android warnings in the log this way.

But...

I would much rather recommend you to simply set the following in your xml:

<EditText android:selectAllOnFocus="true"/>

Like described here. This way your user has a much better UI-feeling to it, he or she can decide on his/her own what to do with the text and won't be annoyed because it clears out every time!

这篇关于一个更好的方式来为的OnClick领域的EditText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆