java - 在数字键盘上单击 "Done"后,常规键盘不断出现

标签 java android android-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 block 内的 onEditorAction 方法来解决此问题:

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

最佳答案

在你的 OnClickListener 上执行此操作

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

关于java - 在数字键盘上单击 "Done"后,常规键盘不断出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18395664/

相关文章:

java - 从 json 响应将字符串转换为 android 中的数组

java - OpenCV Warp 透视图和欧氏距离问题

java - 如何在android后台运行24x7服务来获取用户位置

java - 如何检查edittext是否为空?

java - ImageIO 读取是否意味着抗锯齿缩放?

java - 方法 rxExecuteBlocking 消耗所有结果 - ssh 客户端

javascript - Firebase 项目导致登录时出现 "Auth/network-request-failed"错误

android - Flutter 如何让 uber 或 ola 像在谷歌地图上移动的汽车图标

android EditText 不自动换行文本

android - 更改文本时停止 EditText 滚动 (Android)