2012-05-16 19 views
113

Muszę złapać, gdy EditText traci ostrość, szukałem innych pytań, ale nie znalazłem odpowiedzi.Skąd wiadomo, że EditText traci ostrość?

użyłem OnFocusChangeListener jak ten

OnFocusChangeListener foco = new OnFocusChangeListener() { 

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

    } 
}; 

Ale to nie działa dla mnie.

Odpowiedz

257

Implementacja onFocusChange z setOnFocusChangeListener i istnieje parametr boolowski dla hasFocus. Kiedy jest to fałsz, straciłeś ostrość do innej kontroli.

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 - ale warto zauważyć, że jeśli tekst edycji jest w listview, każdy w dół spowoduje pole utraty i uzyskania ostrości. Zobacz to rozwiązanie, aby śledzić aktualnie zaznaczone pole: http://stackoverflow.com/questions/9527067/android-edittext-in-listview-loses- focus-on-calling-notifydatachanged – bsautner

+13

41 zapisuje odpowiedź, która zasadniczo mówi "przeczytaj wartość" hasFocus "". Prawda jest taka, że ​​naprawdę trudno jest zmusić editText do skupienia się na tym zagadnieniu i jest mnóstwo pytań bez odpowiedzi. –

+0

Gdzie mogę dodać kod, który wyświetlasz? Jeśli umieści go tak, jak jest w "onCreate", aplikacja się zawiesza – Jona

6

Wyraź swoją Activity wdrożyć OnFocusChangeListener() jeśli chcesz factorized korzystania z tego interfejsu, przykład:

public class Shops extends AppCompatActivity implements View.OnFocusChangeListener{ 

w twojej OnCreate można dodać słuchacza na przykład:

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

wtedy studio android poprosi cię o dodanie metody z interfejsu, zaakceptuj ją ... t będzie jak:

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

i jak masz kod factorized, to po prostu trzeba to zrobić:

@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:\""); 
    } 
} 

cokolwiek kod w !hasFocus jest zachowanie przedmiot, który traci ostrość, powinien wystarczyć! Ale uwaga: w takim stanie zmiana fokusu może nadpisać wpisy użytkownika!

0

Jego działa prawidłowo

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

} 
Powiązane problemy