单击数字键盘上的“完成”后,常规键盘会一直显示

时间:2013-08-23 05:50:54

标签: java android android-edittext

我有一个需要输入整数的edittext。因此,我将其设置为仅接受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;
    }

编辑: 我通过在if actionId块中的onEditorAction方法中添加以下代码来修复发布的内容:

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

1 个答案:

答案 0 :(得分:1)

在OnClickListener上执行此操作

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