【问题标题】:Android imeOptions="actionDone" not workingAndroid imeOptions="actionDone" 不起作用
【发布时间】:2015-01-16 09:25:12
【问题描述】:

我正在尝试获取 Android 应用程序的登录屏幕,到目前为止,这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Username"
            android:inputType="text"
            android:singleLine="true"
            android:imeOptions="actionNext">

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textPassword"
            android:singleLine="true"
            android:imeOptions="actionDone"  />

        <Button
            android:id="@+id/buttonLaunchTriage"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="@string/login" />
    </LinearLayout>



</RelativeLayout>

当我尝试运行它时,键盘会显示正确的键,但是当我在输入密码后尝试按完成时,没有任何反应。我正在使用它来处理按钮按下:

private void setupLoginButton() {
    Button launchButton = (Button) findViewById(R.id.buttonLaunchTriage);
    launchButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            EditText username = (EditText) findViewById(R.id.patient_start_userName_value);
            EditText password = (EditText) findViewById(R.id.patient_start_password_value);

            try {
                if(TriageApplicationMain.validateUser(username.getText().toString(),password.getText().toString(),getApplicationContext()))
                {
                Toast.makeText(StartActivity.this,
                        "Launching Triage Application", Toast.LENGTH_SHORT)
                        .show();
                startActivity(new Intent(StartActivity.this, MainActivity.class));
                }

                else
                     {
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            StartActivity.this);


                        // set dialog message
                        alertDialogBuilder
                            .setMessage("Incorrect Credentials")
                            .setCancelable(false)
                            .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, close
                                    // current activity
                                dialog.cancel();    
                                }
                              });


                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();

                    }
                }


            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

}

我知道这是很多代码,但如果有人可以在这里帮助我,那就太好了。这是一个学校项目。

PS:在发布此内容之前,我已经在 Google 上搜索了整整一个小时,所以请不要批评我没有这样做。如果您发现有用的链接,请分享。

【问题讨论】:

  • 您对软输入键盘中​​的“完成”有何期待?隐藏键盘本身还是继续登录?
  • 我也遇到了同样的问题,你必须补充:android:imeActionLabel="Done" android:singleLine="true" 这里是答案stackoverflow.com/questions/5578991/…

标签: android imeoptions


【解决方案1】:

只需将 android:inputType="..." 添加到您的 EditText。它会工作的! :)

【讨论】:

    【解决方案2】:

    您应该为 EditText 设置 OnEditorActionListener 以实现当用户单击键盘中的“完成”时要执行的操作。

    因此,您需要编写如下代码:

    password.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // Do whatever you want here
                return true;
            }
            return false;
        }
    });
    

    android developer site查看教程

    【讨论】:

      【解决方案3】:

      芊芊是对的。您的代码仅侦听按钮单击事件,而不侦听 EditorAction 事件。

      我想补充一点,一些手机供应商可能无法正确实施 DONE 操作。例如,我用联想 A889 进行了测试,当你按下完成时,该手机从不发送 EditorInfo.IME_ACTION_DONE,它总是发送 EditorInfo.IME_ACTION_UNSPECIFIED,所以我实际上最终得到了类似

      myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event)
        {
          boolean handled=false;
      
          // Some phones disregard the IME setting option in the xml, instead
          // they send IME_ACTION_UNSPECIFIED so we need to catch that
          if(EditorInfo.IME_ACTION_DONE==actionId || EditorInfo.IME_ACTION_UNSPECIFIED==actionId)
          {
            // do things here
      
            handled=true;
          }
      
          return handled;
        }
      });
      

      还要注意“handled”标志(Qianqian 没有解释那部分)。可能是其他更高级别的 OnEditorActionListener 正在侦听不同类型的事件。如果您的方法返回 false,则意味着您没有处理此事件,它将被传递给其他人。如果您返回 true,则表示您处理/使用了它,并且不会传递给其他人。

      【讨论】:

      • 谢谢我得到这个 IME_ACTION_UNSPECIFIED 而不是预期的 IME_ACTION_SEND
      【解决方案4】:

      然后使用android:inputType="Yours"

      android:lines="1"
      android:imeOptions="actionDone"
      

      【讨论】:

      • 这行得通。请注意我使用了android:singleLine="true" 而不是android:lines="1"
      【解决方案5】:

      在尝试了很多事情之后,这对我有用:

          android:maxLines="1"
          android:inputType="text"
          android:imeOptions="actionDone"
      

      【讨论】:

      • android:singleLine="true" 也会有所帮助。
      【解决方案6】:

      只需将其添加到您的属性中:

      android:inputType="textPassword"
      

      文档:here

      【讨论】:

      • 对我来说 android:singleLine="true" 成功了,我猜 inputtype=textpassword 强制单行为真......这有点道理。
      【解决方案7】:

      您应该为 EditText 设置 OnEditorActionListener 以实现当用户单击键盘中的“完成”时要执行的操作。

      【讨论】:

        【解决方案8】:

        只是为了添加到倩倩和peedee的答案:

        以下是任何输入事件的操作 ID 为 EditorInfo.IME_ACTION_UNSPECIFIED (0) 的原因。

        actionId int:动作的标识符。这将是您提供的标识符,或者是 EditorInfo#IME_NULL(如果由于按下 Enter 键而被调用)。

        来源:Android documentation

        所以处理回车键事件的正确方法是:

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_NULL) {
                // Handle return key here
                return true;
            }
            return false;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-14
          • 2017-07-17
          • 1970-01-01
          • 2015-05-25
          • 1970-01-01
          • 1970-01-01
          • 2017-07-30
          • 1970-01-01
          相关资源
          最近更新 更多