2013-02-09 70 views
5

我閱讀了關於如何設置要聚焦的對象的問題,但我似乎無法找到任何我想要做的答案。如何強制重點編輯文本

使用焦點監聽我也做了以下內容:

Ehour.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View arg0, boolean arg1) { 
      // TODO Auto-generated method stub 
      if (!arg1) { 
       if (Ehour.getText().length()<=0) { 
        Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show(); 
        Calendar nohour = Calendar.getInstance(); 
        Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY))); 
Ehour.setFocusable(true); 
Ehour.requestFocus(); 


       } 
      } 
     }}); 

我曾嘗試建議從如下職位:

我看到這篇文章,但使用這些建議也似乎不工作: How to set focus on a view when a layout is created and displayed?

我的目標是,當編輯文本焦點丟失時,如果給定的條目無效,我需要回到它。

此外按照由Bill莫特給出的建議我也嘗試:

Ehour.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View arg0, boolean arg1) { 
      // TODO Auto-generated method stub 
      if (!arg1) { 
       if (Ehour.getText().length()<=0) { 
        Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show(); 
        Calendar nohour = Calendar.getInstance(); 
        Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY))); 

        ((EditText)arg0).requestFocus(); 

       } 
      } 
     }}); 

**編輯** 我連requestFocus()方法之前加入下面的行:

((EditText)arg0).setFocusable(true); 

***編輯****

從這篇文章中嘗試:Stop EditText from gaining focus at Activity startup

以下變化: 。在第一種佈局標頭的佈局我說:

android:id="@+id/enterdata" 
android:descendantFocusability="beforeDescendants" 
android:focusableInTouchMode="true" 

然後在OnChangeFocusListener我這樣做:

Ehour.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View arg0, boolean arg1) { 
      // TODO Auto-generated method stub 
      if (!arg1) { 
       if (((EditText)arg0).getText().length()<=0) { 
        Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show(); 
        Calendar nohour = Calendar.getInstance(); 
        ((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY))); 
        findViewById(R.id.enterdata).requestFocus(); 
        ((EditText)arg0).setFocusable(true); 
        ((EditText)arg0).setFocusableInTouchMode(true); 
        ((EditText)arg0).requestFocus(); 

       } 
      } 
     }}); 

不過我還是像以前一樣獲得相同的行爲,焦點轉到的編輯文本仍然保持這種狀態,但我希望焦點轉到的編輯文本看上去很有針對性,但它似乎無法用作好像仍在等待聚焦。

***編輯****下面

代碼按計算器上哪裏誰問的問題用戶遇到同樣的問題,因爲我發現最近的文章:

Ehour.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View arg0, boolean arg1) { 
      // TODO Auto-generated method stub 
      if (!arg1) { 
       if (((EditText)arg0).getText().length()<=0) { 

        Calendar nohour = Calendar.getInstance(); 
        ((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY))); 
        if (((EditText)arg0).requestFocus()) { 
         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
         Emin.clearFocus(); 
         Toast.makeText(getApplicationContext(), "No Hour Entered", Toast.LENGTH_LONG).show(); 
        } 


       } 
      } 
     }}); 
+0

看這個帖子:HTTP://計算器。com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup我嘗試了我將添加到我的問題的答案 – TimCS 2013-02-10 09:10:26

+0

我甚至試圖硬編碼所有編輯文本框以清除焦點並將setFocusable和setFocusableInTouchMode設置爲false,但行爲仍然相同,它看起來像編輯文本我希望焦點聚焦,但任何鍵入都會在用戶將移動到的編輯文本中結束!!!!!幫幫我!!!! – TimCS 2013-02-10 10:36:02

+0

它出現在這篇文章中:http://stackoverflow.com/questions/14327412/set-focus-on-edittext?rq = 1用戶有同樣的問題,它看起來並沒有解決,但我要去嘗試在這裏的建議,看看他們是否爲我工作 – TimCS 2013-02-10 15:27:15

回答

3

嘗試arg0.requestFocus()

不知道是否View實現requestFocus()關閉我的頭頂,所以你可能不得不將它施放... ((EditText) arg0).requestFocus()

+0

抱歉應該在我的問題中說過,按照你所建議的方式嘗試,結果與上面提到的相同 – TimCS 2013-02-09 13:32:47

+0

這兩個編輯文本的行爲對我來說顯得很奇怪,因爲我想移回到外觀的那個看起來很集中,並且似乎有一個對它的遊標,但是焦點轉移到的一個也似乎是相同的,即使我「點擊」回到一個我想使用編輯文本,它改變不是這一個 – TimCS 2013-02-09 20:18:00

+0

它在我看來,那裏似乎現在正在解決這個問題。因此,我將不得不在接受記錄時進行驗證檢查,而不是在用戶離開現場時檢查它。這是在Android 2.2上,所以我不確定它是否延伸到4 – TimCS 2013-02-12 12:02:13

0
new OnEditorActionListener(){ 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     editText.requestFocus(); 
     //used ******* return true ****** 
     return **true**; 
    } 
} 
0

我有同樣的問題,這裏的代碼,我用來解決:

EditText editText = (EditText) findViewById(R.id.myTextViewId); 
editText.requestFocus(); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 

最好的問候,