2012-05-16 45 views
113

Tôi cần nắm bắt khi số EditText mất tiêu điểm, tôi đã tìm kiếm các câu hỏi khác nhưng tôi không tìm thấy câu trả lời.Làm cách nào để biết khi nào EditText mất tiêu điểm?

Tôi đã từng như thế này OnFocusChangeListener

OnFocusChangeListener foco = new OnFocusChangeListener() { 

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

    } 
}; 

Tuy nhiên, nó không làm việc cho tôi.

Trả lời

257

Triển khai onFocusChange của setOnFocusChangeListener và có thông số boolean cho hasFocus. Khi điều này là sai, bạn đã mất tập trung vào một điều khiển khác.

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 - nhưng đáng chú ý là nếu chỉnh sửa văn bản của bạn là trong một listview, mỗi phím sẽ cho kết quả trong hộp mất và nhận được tập trung. Xem giải pháp này để theo dõi hộp hiện đang được tập trung: http://stackoverflow.com/questions/9527067/android-edittext-in-listview-loses-focus-on-calling-notifydatachanged – bsautner

+13

41 phiếu bầu cho câu trả lời cơ bản nói "đọc giá trị 'hasFocus'". Sự thật là điều này thực sự khó khăn để làm cho editText mất tập trung và có rất nhiều câu hỏi chưa được trả lời về nó. –

+0

Tôi sẽ thêm mã vào đâu? Nếu tôi đặt nó như là trong "onCreate" các sự cố ứng dụng – Jona

6

Activity của bạn thực hiện OnFocusChangeListener() nếu bạn muốn có một sử dụng factorized của giao diện này, dụ:

public class Shops extends AppCompatActivity implements View.OnFocusChangeListener{ 

Trong bạn OnCreate bạn có thể thêm một người biết lắng nghe ví dụ:

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

sau đó android studio sẽ nhắc bạn thêm phương thức từ giao diện, chấp nhận nó ... i t sẽ như thế nào:

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

và như bạn đã có một mã factorized, bạn sẽ chỉ phải làm điều đó:

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

bất cứ điều gì bạn mã trong !hasFocus là dành cho hành vi của các mục mà mất tập trung, điều đó nên làm các trick! Nhưng hãy cẩn thận rằng trong trạng thái như vậy, sự thay đổi tập trung có thể ghi đè lên các mục của người dùng!

0

của nó làm việc đúng

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

} 
Các vấn đề liên quan