0

我有一个需要输入整数的编辑文本。因此,我将其设置为仅接受 xml 文件中“数字”的 inputType。在我的代码中,我使用了 onEditorAction 来监听用户单击“完成”。这是分数被“记录”的要求。但无论出于何种原因,每次在数字键盘上点击“完成”时,默认的 qwerty 键盘都会出现并保持焦点。我必须点击“返回”按钮才能摆脱它!

为什么会这样?这是操作编辑文本的所有代码:

从.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;
    }

编辑:我通过将以下代码添加到 if actionId 块中的 onEditorAction 方法来修复问题:

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

1 回答 1

1

在您的 OnClickListener 上执行此操作

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
于 2013-08-23T05:59:30.217 回答