2012-02-10 126 views
0

我是一個新的android編程,我一直在尋找這個小時的答案。我試圖在有人點擊它時清除EditText,但由於某種原因,eclipse並未識別onClickListener。 我錯過了什麼?清除Edittext onclick問題

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); 
    } 
} 
+0

你是不是在你的活動這就是爲什麼它是實現onClickListner的文本的onClick不承認這個Listner。 – 2012-02-10 09:42:42

回答

3

清除上Click事件上的文字EditText是不是一個好辦法UserExperience。

即使您成功將onClick聽衆放在EditText上,您也將面臨焦點和鍵盤未出現的問題。

您可以使用RelativeLayout並將EditText作爲中心,並將文字按鈕"x"RelativeLayout這樣右對齊。按鈕,會出現EditText以上,並且可以由尋求與一些小的調整不錯..

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

明確Button「X」

+0

+1 Nice Adil ... – MKJParekh 2012-02-10 09:35:37

0

試試這個。

text.setonClickListener(new OnClickListener(){ 
    public void OnClick(){ 
      text.setText(""); 
     } 
}; 
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);    
     } 
    }); 
} 
... 
1

我想你搞錯寫的EditText的onClick事件的名稱。 還沒有實現OnClickListener

你已經寫edittext.setText(""),而你有申報的EditText名文本 ..所以在寫的onClick text.setText("")

編輯:

此外,我建議你也應該寫在點擊檢查哪個視圖點擊(雖然在你的情況下,你只用於一個視圖)

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

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

在你的onClick你應該操縱View對象你作爲參數:

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

而且,你的類實現OnClickListener?

public class PillbugsStart extends Activity implements OnClickListener { 
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 

});