清除Edittext onclick问题

时间:2012-02-10 09:16:53

标签: android eclipse

我对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);
    }   
}

6 个答案:

答案 0 :(得分:3)

清除EditText上的Click on Click事件不是一个好的UserExperience方法。

即使您成功将onClick听众放在EditText上,您也会遇到焦点和键盘无法出现的问题。

您可以使用RelativeLayout将其包裹起来,并将EditText作为中心,将"x"文本按钮与RelativeLayout中的EditText对齐,如下所示。按钮将显示在_________________________ | ___________________ | | |_________________|X| | |________________________|<-----RelativeLayout 上方,并且可以通过轻微调整使其看起来很漂亮..

Button

清除{{1}}“X”的文字onClick

答案 1 :(得分:1)

我认为你错误地在onClick事件中写了EditText的名字。 并且没有实施OnClickListener

您在声明EditText名称文字时已撰写edittext.setText("") ..所以在onClick中写text.setText("")

修改

此外,我建议您还应该在onClick上写条件以检查单击了哪个视图(虽然在您的情况下您只使用了一个视图)

@Override 
public void onClick(View v) { 
       int id = v.getId();
       switch(id)
       {
          case R.id.first:
          //code
          break;

          case R.id.second;
          //code
          break;
       }
}

答案 2 :(得分:1)

在onClick中,你应该操作你得到的View对象作为参数:

@Override
public void onClick(View v) {
    v.setText("");
}

此外,您的类是否正在实现OnClickListener?

    public class PillbugsStart extends Activity implements OnClickListener {

答案 3 :(得分:0)

尝试这样。

text.setonClickListener(new OnClickListener(){
    public void OnClick(){
          text.setText("");
      }
};

答案 4 :(得分:0)

尝试使用:

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

答案 5 :(得分:0)

而不是onClickListener尝试使用textChangeListener

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

});