2013-08-23 86 views
0

我有一個需要輸入整數的編輯文本。所以,我將它設置爲只接受xml文件中的「number」的inputType。在我的代碼中,我使用了onEditorAction來監聽用戶單擊「完成」。這是要求「記錄」分數的要求。但無論出於何種原因,每次在數字鍵盤上點擊「完成」,默認的QWERTY鍵盤就會出現並保持焦點。我必須點擊「後退」按鈕才能擺脫它!點擊數字鍵盤上的「完成」後,常規鍵盤不斷顯示

這是怎麼發生的?這裏的一切操縱的EditText代碼:

從.XML:

<EditText 
     android:id="@+id/partial_score_entry" 
     android:layout_width="100dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:inputType="number" 
     android:hint="0" /> 

自定義適配器:

... 
public View getView(int pos, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View v = convertView; 
     if (v == null) 
     { 
      LayoutInflater vi = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.score_list_row_2, null); 
     } 

      TextView subtaskView = (TextView)v.findViewById(R.id.subtask2); 
      TextView maxPointsView = (TextView)v.findViewById(R.id.max_points2); 

      final ScoringInfo scr = data.get(pos); 
      subtaskView.setText(scr.subtask); 
      maxPointsView.setText("Max Points: " + Integer.toString(scr.maxPoints)); 

      final EditText manualScore = (EditText)v.findViewById(R.id.partial_score_entry); 
      manualScore.setTag(R.string.score, scr.maxPoints); 
      manualScore.setTag(R.string.subtask_num, scr.subtaskNum); 
      manualScore.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        try { 
         previous = Integer.parseInt(manualScore.getText().toString()); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 

      }); 
      manualScore.setOnEditorActionListener(new OnEditorActionListener() { 

       @Override 
       public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {     

        if (actionId == EditorInfo.IME_ACTION_DONE) { 
         scr.addToTotalPoints(-previous); 

         int max = Integer.parseInt(manualScore.getTag(R.string.score).toString()); 
         int subNum = Integer.parseInt(manualScore.getTag(R.string.subtask_num).toString()); 
         Challenge.subtaskScores.remove(subNum); 
         int tmpScore = 0; 

         if (!manualScore.getText().toString().equals("")) { 
          try { 
           tmpScore = Integer.parseInt(manualScore.getText().toString()); 
          } catch (Exception e) { 
           e.printStackTrace(); 
           Toast.makeText(c, "Error: Maximum point allowance surpassed.", Toast.LENGTH_SHORT).show(); 
          } 
          if (tmpScore > max){ 
           Toast.makeText(c, "Error: Maximum point allowance surpassed.", Toast.LENGTH_SHORT).show(); 
           manualScore.setText(""); 
          } 
          else { 
           Challenge.subtaskScores.put(subNum, tmpScore); 
           scr.addToTotalPoints(tmpScore); 
           updatePoints(scr.getTotalPoints()); 
          } 
         } 

          manualScore.clearFocus(); 

          return true; 
        } 
        return false; 
       } 
      }); 

      manualScore.setOnFocusChangeListener(new View.OnFocusChangeListener() 
      { 
       @Override 
       public void onFocusChange(View v, boolean hasFocus) 
       { 
        if(hasFocus) 
        { 
         if(!manualScore.getText().toString().equals("")) 
          previous = Integer.parseInt(manualScore.getText().toString()); 
        } 
       } 
      }); 

     return v; 
    } 

編輯: 我固定加入以下代碼到onEditorAction發行方法在if actionId塊內:

InputMethodManager imm = (InputMethodManager)c.getSystemService(Context.INPUT_METHOD_SERVICE); 
         imm.hideSoftInputFromWindow(manualScore.getWindowToken(), 0); 

回答

1

O n你的OnClickListener做到這一點

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(getWindowToken(), 0); 
+0

它說,INPUT_METHOD_SERVICE不能解析爲變量。 –

+0

好的,我試着用這兩種上下文。 (c)中定義的上下文:InputMethodManager imm =(InputMethodManager)c.getSystemService(c.INPUT_METHOD_SERVICE);它仍然沒有區別。 –

+0

對不起,連續3條評論,但我想通了。我將在我的OP的解決方案中進行編輯。謝謝。你的代碼讓我朝着正確的方向前進,終於想出瞭如何去做。 –