Ent*_*024 69 android android-edittext
在Android中如何EditText在点击时清楚地表明?
例如,如果我有EditText一些字符,例如'Enter Name',当用户点击它时,这些字符就会消失.
Spe*_*cur 181
我不确定你是否在此之后,但试试这个XML:
android:hint="Enter Name"
Run Code Online (Sandbox Code Playgroud)
当输入字段为空,选中或未选中时,它会显示该文本.
或者,如果您希望它完全按照您的描述执行,请在editText上分配onClickListener并使用setText()将其设置为空.
Har*_*ris 37
您是否正在寻找类似于x的行为,该行为显示在iPhone上文本字段的右侧,在点按时会清除文本?它在那里被称为clearButtonMode.以下是如何在Android EditText视图中创建相同的功能:
String value = "";//any text you are pre-filling in the EditText
final EditText et = new EditText(this);
et.setText(value);
final Drawable x = getResources().getDrawable(R.drawable.presence_offline);//your x image, this one from standard android images looks pretty good actually
x.setBounds(0, 0, x.getIntrinsicWidth(), x.getIntrinsicHeight());
et.setCompoundDrawables(null, null, value.equals("") ? null : x, null);
et.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (et.getCompoundDrawables()[2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
if (event.getX() > et.getWidth() - et.getPaddingRight() - x.getIntrinsicWidth()) {
et.setText("");
et.setCompoundDrawables(null, null, null, null);
}
return false;
}
});
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.setCompoundDrawables(null, null, et.getText().toString().equals("") ? null : x, null);
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
Run Code Online (Sandbox Code Playgroud)
Rah*_*dia 27
点击下面的任何动作后执行
((EditText) findViewById(R.id.yoursXmlId)).setText("");
Run Code Online (Sandbox Code Playgroud)
要么
在XML文件中写这个
<EditText
---------- other stuffs ------
android:hint="Enter Name" />
Run Code Online (Sandbox Code Playgroud)
它对我来说很好.希望你们所有人.
Car*_*s P 19
@ Harris的答案很棒,我已经将它实现为EditText的一个独立子类,如果你的代码已经添加了TextChangedListeners,它可以更容易使用.
此外,我已经调整了它,如果你已经使用任何复合Drawables,它会保持原样.
代码在这里,对于任何需要它的人:
package com.companyname.your
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
public class ClearableEditText extends EditText {
public String defaultValue = "";
final Drawable imgX = getResources().getDrawable(android.R.drawable.presence_offline ); // X image
public ClearableEditText(Context context) {
super(context);
init();
}
public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ClearableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
void init() {
// Set bounds of our X button
imgX.setBounds(0, 0, imgX.getIntrinsicWidth(), imgX.getIntrinsicHeight());
// There may be initial text in the field, so we may need to display the button
manageClearButton();
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ClearableEditText et = ClearableEditText.this;
// Is there an X showing?
if (et.getCompoundDrawables()[2] == null) return false;
// Only do this for up touches
if (event.getAction() != MotionEvent.ACTION_UP) return false;
// Is touch on our clear button?
if (event.getX() > et.getWidth() - et.getPaddingRight() - imgX.getIntrinsicWidth()) {
et.setText("");
ClearableEditText.this.removeClearButton();
}
return false;
}
});
this.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ClearableEditText.this.manageClearButton();
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
}
void manageClearButton() {
if (this.getText().toString().equals("") )
removeClearButton();
else
addClearButton();
}
void addClearButton() {
this.setCompoundDrawables(this.getCompoundDrawables()[0],
this.getCompoundDrawables()[1],
imgX,
this.getCompoundDrawables()[3]);
}
void removeClearButton() {
this.setCompoundDrawables(this.getCompoundDrawables()[0],
this.getCompoundDrawables()[1],
null,
this.getCompoundDrawables()[3]);
}
}
Run Code Online (Sandbox Code Playgroud)
Sco*_*ler 15
如果您想在编辑文本中包含文本并将其删除,请尝试:
final EditText text_box = (EditText) findViewById(R.id.input_box);
text_box.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus==true)
{
if (text_box.getText().toString().compareTo("Enter Text")==0)
{
text_box.setText("");
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
在设置文本的字段上使用onClick侦听器设置文本时要小心.我这样做并将文本设置为空字符串.这导致指针出现以指示我的光标所在的位置,这通常会在几秒钟后消失.当我离开我的页面导致finish()被调用之前我没有等待它消失时,它会导致内存泄漏并导致我的应用程序崩溃.我花了一些时间来弄清楚是什么导致了这个崩溃.
无论如何,如果可以,我建议在你的点击监听器而不是setText()中使用selectAll().这样,一旦选择了文本,用户就可以开始输入,并且将清除所有先前的文本.
可疑指针的图片:http://i.stack.imgur.com/juJnt.png
| 归档时间: |
|
| 查看次数: |
131863 次 |
| 最近记录: |