2014-04-09 161 views
0

我有一個EditText,其中EditText獲取焦點,它改變EditText的功能,當我失去對EditText的焦點時,它將再次改變函數。事件聚焦於EditText

這是我的源代碼:

​​

}

對於喜歡當我點擊的EditText首次結果,EditText上沒有做的事情。當我輸入「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

啊..所以它不關注焦點。但是當你第二次點擊editText時,你仍然會關注editText。如果你想按照你現在告訴我的方式去做,你需要用一面旗子來控制它。 –