45

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 で 1 時間ほど検索したので、そうしないことを批判しないでください。役に立つリンクを見つけたら、共有してください。

4

8 に答える 8

46

ユーザーがキーボードで「完了」をクリックしたときに実行するアクションを実装するには、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 開発者サイトのチュートリアルを参照してください

于 2014-11-19T01:38:47.127 に答える
25

Qianqianは正しいです。コードは、EditorAction イベントではなく、ボタン クリック イベントのみをリッスンします。

一部の電話ベンダーは DONE アクションを適切に実装していない可能性があることを付け加えておきます。たとえば、Lenovo 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;
  }
});

また、「処理済み」フラグにも注意してください (Qianqian はその部分について説明していません)。上位の他の OnEditorActionListeners が異なるタイプのイベントをリッスンしている可能性があります。メソッドが false を返す場合は、このイベントを処理しなかったことを意味し、他のユーザーに渡されます。true を返す場合は、それを処理/消費したことを意味し、他の人には渡されません。

于 2014-11-19T02:10:28.320 に答える
3

これを属性に追加するだけです:

android:inputType="textPassword"

ドキュメント:こちら

于 2018-05-07T16:52:02.693 に答える