2012-05-16 3 views
113

EditTextがフォーカスを失ったときにキャッチする必要があります。私は他の質問を検索しましたが、回答が見つかりませんでした。EditTextがフォーカスを失ったときは、どうすればわかりますか?

私はそれが私のために動作しません、OnFocusChangeListener

このような
OnFocusChangeListener foco = new OnFocusChangeListener() { 

    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     // TODO Auto-generated method stub 

    } 
}; 

を使用しかし。

答えて

257

onFocusChangesetOnFocusChangeListenerを実装し、hasFocusのブール型パラメータがあります。これが間違っていると、別のコントロールにフォーカスが失われました。

EditText txtEdit = (EditText) findViewById(R.id.edittxt); 

txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {   
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus) { 
       // code to execute when EditText loses focus 
      } 
     } 
    }); 
+15

1 - しかし、あなたのエディットテキストがリストビューにある場合、すべてのキーダウンボックスが失うと、フォーカスを得ることになりますことは注目に値します。現在フォーカスされているボックスを追跡するには、このソリューションを参照してください:http://stackoverflow.com/questions/9527067/android-edittext-in-listview-loses-focus-on-calling-notifydatachanged – bsautner

+13

41 upvotesは、基本的に答えています"hasFocus 'の値を読んでください。真実は、これがeditTextにフォーカスを失わせることは本当に難しく、未解決の疑問がたくさんあることです。 –

+0

表示するコードはどこに追加しますか?私はそれを "onCreate"に置くとアプリがクラッシュする – Jona

6

あなたのあなたは、このインターフェースの因数分解を使用したい場合はActivityOnFocusChangeListener()を実装 例持っている:あなたのOnCreateあなたはたとえば、リスナーを追加することができて

public class Shops extends AppCompatActivity implements View.OnFocusChangeListener{ 

editTextReaserch.setOnFocusChangeListener(this); 
editTextMyWords.setOnFocusChangeListener(this); 
editTextPhone.setOnFocusChangeListener(this); 

をアンドロイドスタジオは、インターフェイスからメソッドを追加するように求め、それを受け入れます... 私はTは次のようになります:

@Override 
public void onFocusChange(View v, boolean hasFocus) { 
// todo your code here... 
} 

、あなたは因数分解のコードを持っているとして、あなたはちょうどそれを行う必要があるでしょう:

@Override 
public void onFocusChange(View v, boolean hasFocus) { 
    if (hasFocus) { 
     editTextReaserch.setText(""); 
     editTextMyWords.setText(""); 
     editTextPhone.setText(""); 
    } 
    if (!hasFocus){ 
     editTextReaserch.setText("BlaBlaBla"); 
     editTextMyWords.setText(" One Two Tree!"); 
     editTextPhone.setText("\"your phone here:\""); 
    } 
} 

何かをあなたが!hasFocusのコードはの行動のためでありますトリックをする必要がある、フォーカスを失うアイテム!しかし、そのような状態では、フォーカスの変更によってユーザーのエントリが上書きされる可能性があることに注意してください。

0

その作業を適切に

EditText et_mobile= (EditText) findViewById(R.id.edittxt); 

et_mobile.setOnFocusChangeListener(new OnFocusChangeListener() {   
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       // code to execute when EditText loses focus 
if (et_mobile.getText().toString().trim().length() == 0) { 
         CommonMethod.showAlert("Please enter name", FeedbackSubmtActivity.this); 
        } 
      } 
     } 
    }); 



public static void showAlert(String message, Activity context) { 

    final AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setMessage(message).setCancelable(false) 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }); 
    try { 
     builder.show(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 
関連する問題