输入时清除 EditText 中的文本

Clear text in EditText when entered(输入时清除 EditText 中的文本)

本文介绍了输入时清除 EditText 中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置和 onclicklistener,以便当我在 edittext 元素中单击时,它将清除其当前内容.这里有什么问题吗?当我编译这段代码时,我得到一个强制退出和 ActivityManager: Can't dispatch DDM chunk 4d505251: no handler defined 错误.

I'm trying to set and onclicklistener so that when I click within the edittext element it will clear its current contents. Is there something wrong here? When I compile this code I get a force quit and ActivityManager: Can't dispatch DDM chunk 4d505251: no handler defined error.

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) 然后所有其他的初始化.

First you need to call setContentView(R.layout.main) then all other initialization.

请尝试以下代码.

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.getText().clear(); //or you can use editText.setText("");
    }
}

这篇关于输入时清除 EditText 中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:输入时清除 EditText 中的文本

基础教程推荐