input时在EditText中清除文本

我试图设置和onclicklistener,所以当我点击edittext元素,它将清除其当前内容。 这里有什么不对吗? 当我编译此代码时,我得到一个强制退出和ActivityManager:无法调度DDM块4d505251:没有处理程序定义的错误。

public class Project extends Activity implements OnClickListener { /** Called when the activity is first created. */ EditText editText = (EditText)findViewById(R.id.editText1); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); editText.setOnClickListener(this); setContentView(R.layout.main); } @Override public void onClick(View v) { // TODO Auto-generated method stub editText.setText(""); } } 

首先你需要调用setContentView(R.layout.main)然后所有其他的初始化。

请尝试下面的代码。

 public class Trackfolio extends Activity implements OnClickListener { /** Called when the activity is first created. */ public EditText editText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.editText1); editText.setOnClickListener(this); } @Override public void onClick(View v) { editText.setText(""); } } 

你也可以使用下面的代码

 editText.getText().clear(); 

只需在EditText中使用android:hint属性即可。 当文本框为空并且没有被聚焦时,该文本显示,但是在selectEditText框时消失。

你的代码应该是:

  public class Project extends Activity implements OnClickListener { /** Called when the activity is first created. */ EditText editText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText)findViewById(R.id.editText1); editText.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == editText) { editText.setText(""); } } } 
 public EditText editField; public Button clear = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text_layout); this. editField = (EditText)findViewById(R.id.userName); this.clear = (Button) findViewById(R.id.clear_button); this.editField.setOnClickListener(this); this.clear.setOnClickListener(this); @Override public void onClick(View v) { // TODO Auto-generated method stub if(v.getId()==R.id.clear_button){ //setText will remove all text that is written by someone editField.setText(""); } } 
 package com.example.sampleproject; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class SampleProject extends Activity { EditText mSearchpeople; Button mCancel , msearchclose; ImageView mprofile, mContact, mcalender, mConnection, mGroup , mFollowup , msetting , mAddacard; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dashboard); mSearchpeople = (EditText)findViewById(R.id.editText1); mCancel = (Button)findViewById(R.id.button2); msearchclose = (Button)findViewById(R.id.button1); mprofile = (ImageView)findViewById(R.id.imageView1); mContact = (ImageView)findViewById(R.id.imageView2); mcalender = (ImageView)findViewById(R.id.imageView3); mConnection = (ImageView)findViewById(R.id.imageView4); mGroup = (ImageView)findViewById(R.id.imageView5); mFollowup = (ImageView)findViewById(R.id.imageView6); msetting = (ImageView)findViewById(R.id.imageView7); mAddacard = (ImageView)findViewById(R.id.imageView8); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); mCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mSearchpeople.clearFocus(); } }); } } 

非常简单的清除editText values.when你点击button,然后只遵循1行代码。

里面的button或任何你想要的。只用这个

 editText.setText(""); 

//清除单击清除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即可清除所有内容。它为我工作。 希望能帮助到你

我不知道在执行上述解决scheme时犯了什么错误,但是他们对我没有成功

 txtDeck.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { txtDeck.setText(""); } }); 

这对我有用,

 final EditText childItem = (EditText) convertView.findViewById(R.id.child_item); childItem.setHint(cellData); childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { //Log.d("NNN", "Has focus " + hasFocus); if (hasFocus) { Toast.makeText(ctx.getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show(); } else { Toast.makeText(ctx.getApplicationContext(), "loss the focus", Toast.LENGTH_SHORT).show(); } return; }); 

通过设置空string,你可以清除你的编辑文本

  editext.setText("");