2012-02-10 13 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

のテキストのonClick明らか

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

.. EditTextの上に表示されますとマイナーな調整で見栄えの良い行うことができますこのListnerを認識しません。 –

答えて

3

を使用してみてください良いUserExperienceのアプローチではありません。

onClickリスナーをEditTextに配置しても、フォーカスとキーボードが表示されないという問題が発生します。

あなたはRelativeLayoutを使用して、それを周りにラップすることができますし、このようなRelativeLayoutで右に並ぶ"x"などのテキストと中央とボタンとしてEditTextを置きます。ボタンをクリックすると、それが理由です、あなたの活動にonClickListnerを実装していないButton「X」

+0

+1 Nice Adil ... – MKJParekh

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

私はあなたがonClickイベントでのEditTextの名前を書くことは間違っていたと思います。 ともあなたはonClickの中テキスト ..soがtext.setText("")

EDIT書き込みのEditText名を宣言している間、あなたはedittext.setText("")を書かれているOnClickListener

実装されていません:

また、私はあなたにも書くべきアドバイスをどのビューがクリックされたかを確認するにはonClickの条件(1つのビューにしか使用していませんが)

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

      case R.id.second; 
      //code 
      break; 
     } 
} 
あなたのonClickあなたは、パラメータとして取得するViewオブジェクトを操作する必要がありで
1

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

をまた、OnClickListenerを実装するクラスですか?代わりにonClickListenerの

public class PillbugsStart extends Activity implements OnClickListener { 
0

EditText上のClickイベントのテキストをクリア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 

});