Android Clear Edittext onclick问题

Android Clear Edittext onclick问题,android,eclipse,Android,Eclipse,我对android编程有点陌生,几个小时来我一直在寻找答案。我试图在有人点击时清除EditText,但出于某种原因,eclipse无法识别onClickListener。 我错过了什么 public class PillbugsStart extends Activity { private EditText text; private TextView f; @Override public void onCreate(Bundle save

我对android编程有点陌生,几个小时来我一直在寻找答案。我试图在有人点击时清除
EditText
,但出于某种原因,eclipse无法识别
onClickListener
。 我错过了什么

    public class PillbugsStart extends Activity {

        private EditText text;
    private TextView f;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.pillbugsstart);

        text = (EditText) findViewById(R.id.editText1);
        text.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {

        editText.setText("");
        f = (TextView) findViewById(R.id.grah);

            ImageButton b = (ImageButton)findViewById(R.id.testbutton2);
            b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View argo) {

            Intent j = new Intent(PillbugsStart.this, Startpage.class);
            PillbugsStart.this.startActivity(j);            
            }
        });
      } 
      public void myClickHandler(View view) {

                switch (view.getId()) {
        case R.id.FBLUB:

            RadioButton pounds = (RadioButton) findViewById(R.id.lbs);
            RadioButton kilograms = (RadioButton) findViewById(R.id.kilograms);
            if (text.getText().length() == 0)
                        {
                Toast.makeText(this, "Please enter a valid number",
                        Toast.LENGTH_LONG).show();
                return;
            }
            float inputValue = Float.parseFloat(text.getText().toString());
            if (pounds.isChecked()) 
                        {
                f.setText(String.valueOf(convertFromPounds(inputValue)));
                pounds.setChecked(true);
            }
                        else 
                        {
                    f.setText(String.valueOf(convertFromKilograms(inputValue)));
            }
            break;
        }

    }   
    private float convertFromPounds(float pounds) 
        {
        return ((pounds * 454) * 1/2);
    }
    private float convertFromKilograms(float kilograms) 
        {
        return ((kilograms * 1000) * 1/2);
    }   
}
像这样试试

text.setonClickListener(new OnClickListener(){
    public void OnClick(){
          text.setText("");
      }
};
像这样试试

text.setonClickListener(new OnClickListener(){
    public void OnClick(){
          text.setText("");
      }
};
尝试使用:

public .... onCreate(){

    ...
    text.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

                    text.setText("");

        }
    });
    ImageButton b = (ImageButton)findViewById(R.id.testbutton2);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View argo) {

            Intent j = new Intent(PillbugsStart.this, Startpage.class);
            PillbugsStart.this.startActivity(j);            
        }
    });
}
...
尝试使用:

public .... onCreate(){

    ...
    text.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

                    text.setText("");

        }
    });
    ImageButton b = (ImageButton)findViewById(R.id.testbutton2);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View argo) {

            Intent j = new Intent(PillbugsStart.this, Startpage.class);
            PillbugsStart.this.startActivity(j);            
        }
    });
}
...

我认为您在onClick事件中写错了EditText的名称。 而且也没有实现
OnClickListener

您已经编写了
edittext.setText(“”
,而您已经声明了edittext nametext。因此,在onClick write
text.setText(“”)

编辑:

此外,我建议您还应该在onClick中编写条件,以检查单击了哪个视图(尽管在您的示例中,您只使用了一个视图)


我认为您在onClick事件中写错了EditText的名称。 而且也没有实现
OnClickListener

您已经编写了
edittext.setText(“”
,而您已经声明了edittext nametext。因此,在onClick write
text.setText(“”)

编辑:

此外,我建议您还应该在onClick中编写条件,以检查单击了哪个视图(尽管在您的示例中,您只使用了一个视图)


在onClick中,应操作作为参数获取的视图对象:

@Override
public void onClick(View v) {
    v.setText("");
}
另外,您的类是否实现了OnClickListener

    public class PillbugsStart extends Activity implements OnClickListener {

在onClick中,应操作作为参数获取的视图对象:

@Override
public void onClick(View v) {
    v.setText("");
}
另外,您的类是否实现了OnClickListener

    public class PillbugsStart extends Activity implements OnClickListener {

尝试使用textChangeListener代替onClickListener

editText.addTextChangedListener(new TextWatcher()
{
      // TextWatcher methods
    public void afterTextChanged(Editable statusText) {
        super.afterTextChanged(statusText);
    } // afterTextChanged

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // not implemented here
        Log.d(TAG, "beforeTextChanged");
                //Clear your text here
    } // beforeTextChanged

    public void onTextChanged(CharSequence s, int start, int count, int after) {
        // not implemented here
        Log.d(TAG, "onTextChanged");
    } // onTextChanged

});

尝试使用textChangeListener代替onClickListener

editText.addTextChangedListener(new TextWatcher()
{
      // TextWatcher methods
    public void afterTextChanged(Editable statusText) {
        super.afterTextChanged(statusText);
    } // afterTextChanged

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // not implemented here
        Log.d(TAG, "beforeTextChanged");
                //Clear your text here
    } // beforeTextChanged

    public void onTextChanged(CharSequence s, int start, int count, int after) {
        // not implemented here
        Log.d(TAG, "onTextChanged");
    } // onTextChanged

});

清除
EditText
上的单击事件文本不是一种好的用户体验方法

即使您成功地将
onClick
侦听器置于
EditText
上,您也将面临焦点和键盘不出现的问题

您可以使用
RelativeLayout
将其环绕,并将
EditText
放在中间,然后在
RelativeLayout
中向右对齐文本为
“x”
的按钮,如下所示。按钮将出现在
EditText
上方,只需稍加调整即可使其看起来漂亮

_________________________
|   ___________________  |
|  |_________________|X| |
|________________________|<-----RelativeLayout
_________________________
|   ___________________  |
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu124; X ||

|________________________| 清除
EditText
上的单击事件文本不是一种好的用户体验方法

即使您成功地将
onClick
侦听器置于
EditText
上,您也将面临焦点和键盘不出现的问题

您可以使用
RelativeLayout
将其环绕,并将
EditText
放在中间,然后在
RelativeLayout
中向右对齐文本为
“x”
的按钮,如下所示。按钮将出现在
EditText
上方,只需稍加调整即可使其看起来漂亮

_________________________
|   ___________________  |
|  |_________________|X| |
|________________________|<-----RelativeLayout
_________________________
|   ___________________  |
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu124; X ||

|________________________|您没有在活动中实现onClickListner,这就是它无法识别此Listner的原因。您没有在活动中实现onClickListner,这就是它无法识别此Listner的原因。