開発者ドキュメント

Androidパスワードフィールドの例

Androidでは、「http://developer.android.com/reference/android/widget/EditText.html[android.widget.EditText」を使用して、 `inputType =” textPassword “`を使用してパスワードコンポーネントをレンダリングできます。

このチュートリアルでは、XMLを使用してパスワードフィールド、ラベルフィールド、および通常のボタンを作成する方法を示します。ボタンをクリックすると、パスワード値がフローティングメッセージ(トーストメッセージ)として表示されます。


P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.

でテストされています

1.カスタム文字列



res/values/strings.xml

“ファイルを開き、デモンストレーション用のカスタム文字列を追加します。

ファイル:res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app__name">MyAndroidApp</string>
    <string name="lblPassword">Enter Your Password :</string>
    <string name="btn__submit">Submit</string>
</resources>

2.パスワード



res/layout/main.xml

“ファイルを開き、パスワードコンポーネント、

EditText`

inputType = “textPassword”を追加してください。


File:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout__width="fill__parent"
    android:layout__height="fill__parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblPassword"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="@string/lblPassword"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <EditText
        android:id="@+id/txtPassword"
        android:layout__width="match__parent"
        android:layout__height="wrap__content"
        android:inputType="textPassword" >

        <requestFocus/>
    </EditText>

    <Button
        android:id="@+id/btnSubmit"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="@string/btn__submit"/>

</LinearLayout>

3.コードコード

アクティビティ “onCreate()` “メソッドの内部で、ボタンのクリックリスナをアタッチしてパスワード値を表示します。


File:MyAndroidAppActivity.java

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

  private EditText password;
  private Button btnSubmit;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

  }

  public void addListenerOnButton() {

    password = (EditText) findViewById(R.id.txtPassword);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

          Toast.makeText(MyAndroidAppActivity.this, password.getText(),
            Toast.LENGTH__SHORT).show();

        }

    });

  }
}

4.デモ

アプリケーションを実行します。

{空} 1。結果、パスワードフィールドが表示されます。



{空} 2。パスワード「mkyong123」を入力し、送信ボタンをクリックします。



ソースコードをダウンロードする

ダウンロードする –

Android-Password-Example.zip

(15 KB)

参考文献

EditText JavaDoc]

リンク://タグ/アンドロイド/[アンドロイド]リンク://タグ/パスワード/[パスワード]

モバイルバージョンを終了