如何清除点击EditText?

在Android中,如何使EditText在被点击时清除?

例如,如果我有一个带有某些字符的EditText ,比如'Enter Name' ,当用户点击它时,这些字符就会消失。

我不确定你是否在这之后,但试试这个XML:

 android:hint="Enter Name" 

当input字段为空时,显示该文本,选中或取消选中。

或者,如果您希望完全按照您所描述的那样操作,请在editText上分配一个onClickListener,并使用setText()将其设置为空。

你是否正在寻找类似于iPhone上的文本字段右侧出现的行为,当点击时清除文本? 这里叫做clearButtonMode。 以下是如何在Android EditText视图中创build相同的function:

 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) { } }); 

在执行下面的任何操作之后执行步骤

 ((EditText) findViewById(R.id.yoursXmlId)).setText(""); 

要么

把它写在XML文件中

 <EditText ---------- other stuffs ------ android:hint="Enter Name" /> 

其作品对我来说很好。 希望大家。

@哈里斯的答案是伟大的,我已经实现它作为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]); } } 

在android中使用android:hint =“Enter Name”

如果您想在编辑文本中添加文本,并像您所说的那样删除它,请尝试:

  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(""); } } } }); 

在设置文本的字段上使用onClick侦听器设置文本时要小心。 我正在这样做,并设置文本为空string。 这导致指针出现,指示我的光标在哪里,几秒钟后通常会消失。 当我没有等到它离开我的页面导致完成()被调用之前,它会导致内存泄漏,并崩溃我的应用程序。 花了我一段时间才弄清楚是什么原因导致了这一次的崩溃..

无论如何,如果可以的话,我build议在你的点击监听器中使用selectAll(),而不是setText()。 这样,一旦select了文本,用户就可以开始input,并且所有的前面的文本都将被清除。

犯罪嫌疑人指针图片: http : //i.stack.imgur.com/juJnt.png

//清除单击清除button时清除

firstName =(EditText)findViewById(R.id.firstName);

  clear = (Button) findViewById(R.id.clearsearchSubmit); clear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (v.getId() == R.id.clearsearchSubmit); firstName.setText(""); } }); 

这将有助于清除您input的错误关键字,而不是一次又一次地按下退格键,只需单击button即可清除所有内容。它为我工作。 希望能帮助到你

单击时清除文本字段的代码

 <EditText android:onClick="TextFieldClicked"/> public void TextFieldClicked(View view){ if(view.getId()==R.id.editText1); text.setText(""); } 
 ((EditText) findViewById(R.id.User)).setText(""); ((EditText) findViewById(R.id.Password)).setText(""); 

对我来说最简单的方法…创build一个公共的EditText,例如“myEditText1”

 public EditText myEditText1; 

然后,连接它应该被清除的EditText

 myEditText1 = (EditText) findViewById(R.id.numberfield); 

之后,创build一个无效的点击EditText,让它清除其中的文本,当它的焦点,例如

 @OnClick(R.id.numberfield) void textGone(){ if (myEditText1.isFocused()){ myEditText1.setText(""); } } 

希望我能帮助你,祝你有个美好的一天