2014-04-09 3 views
0

EditText에서 포커스를 얻는 EditText가 있는데, EditText의 기능이 변경되고 EditText에서 포커스를 잃으면 기능이 다시 변경됩니다.Event Focus on EditText

이 내 소스 코드 :

tambah = (EditText)rootView.findViewById(R.id.total); 
tambah.setOnFocusChangeListener(new OnFocusChangeListener(){ 
@Override 
public void onFocusChange(View arg0, boolean arg1) { 
    try { 
     if(..) //i want to check, i get focus 
     {...}else //if i loss focus, the textbox do something 
     {...} 

    } catch (Exception e) { 
    } 

} 내가 처음 글고 치기를 클릭 할 때와 같은 결과를

의 글고이 일을하지 않습니다. "1000"을 입력하면 포커스가 손실되고 edittext는 "1.000"으로 변경됩니다. 그러나 다시 edittext를 클릭하면 다시 "1000"이됩니다. 도와주세요. 문법이 나쁘면 미안 해요.

답변

0

부울 플래그 = 참;

tambah = (EditText)rootView.findViewById(R.id.about_header); 
      tambah.setOnFocusChangeListener(new OnFocusChangeListener() { 

       public void onFocusChange(View v, boolean hasFocus) { 
        flag = !flag; 
        if(v == tambah && flag) { 
         //format your text to 1000 here 
        } 
        else if(v == tambah && !flag) { 
         //format your text to 1.000 here 
        } 
       } 
      }); 

편집 : 간단한 포맷을해야하는 경우 , 당신은 소수점 포맷을 사용할 수 있습니다.

double d = 1.000; 
DecimalFormat df1 = new DecimalFormat("#.###"); 
DecimalFormat df2 = new DecimalFormat("####"); 
String newD1 = df1.format(d); // 1.000 
String newD2 = df2.format(d); // 1000 
+0

내가 edittext를 두 번 입력 할 때 나는 여전히 "1.000"을 얻습니다. edittext에 "1.000"이 있고 다시 클릭하면 "1000"이 다시 나타납니다. 내가 어떻게 알아? – user3505775

+0

맞아요, 내 자동 생성 된 변수 이름이 다르기 때문에 다행 이네요. –

+0

Ahh .. 초점이 맞지 않아요. 그러나 editText를 두 번 클릭하면 여전히 editText에 초점을 맞 춥니 다. 당신이 지금 나 한테 말한 것처럼 그렇게하고 싶다면, 깃발로 제어해야합니다. –