首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android imeOptions="actionDone“无法工作

Android imeOptions="actionDone“无法工作
EN

Stack Overflow用户
提问于 2014-11-19 00:49:20
回答 8查看 53.6K关注 0票数 45

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

代码语言:javascript
复制
<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>

当我尝试运行它时,键盘显示正确的键,但当我试图在输入密码后按下“完成”时,什么都不会发生。我用它来处理按钮按下:

代码语言:javascript
复制
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上搜索了整整一个小时,然后才发布这篇文章,所以请不要因为没有这么做而批评。如果你找到一个有用的链接,那么请分享。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2017-01-08 06:50:38

只需将android:inputType="..."添加到EditText中即可。这是可行的!)

票数 108
EN

Stack Overflow用户

发布于 2014-11-19 01:38:47

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

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

代码语言:javascript
复制
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开发者站点的教程

票数 46
EN

Stack Overflow用户

发布于 2014-11-19 02:10:28

前迁是对的。您的代码只侦听按钮单击事件,而不是EditorAction事件。

我想补充的是,一些电话供应商可能没有正确地实现已完成的操作。例如,我用联想的A889测试过这个,当你按下完成键时,手机就不会发送EditorInfo.IME_ACTION_DONE,它总是发送EditorInfo.IME_ACTION_UNSPECIFIED,所以我最终得到的结果是

代码语言:javascript
复制
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;
  }
});

还注意到“处理”旗(前谦没有解释那部分)。可能是其他更高的OnEditorActionListeners在监听不同类型的事件。如果您的方法返回false,这意味着您没有处理此事件,并且它将传递给其他人。如果您返回true,这意味着您处理/使用了它,并且它不会传递给其他人。

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27007040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档