marunomaruno-memo

marunomaruno-memo

[Android] 標準ウィジェット(1) EditText

2012年03月03日 | Android
                                                                2012-03-09 更新
                                                                2012-03-03 新規
[Android] 標準ウィジェット(1) EditText
================================================================================

Android で文字列データを入力するには、EditText を使う。

この EditText には、いろいろな入力形式が用意されている。
数字だけに限定することもできれば、email で使う文字だけに限定する、また、パスワー
ドの入力に使う、ということも可能。

入力した文字列は
    Editable   getText() 
メソッドを使うが、返り値は Editable 型。通常は toString() メソッドを使って、テキ
ストにして使う。

レイアウトの EditText 要素の android:inputType 属性を使ってこれらを制御する。

------------------- ------------------------------------------------------------
android:inputType 属性
------------------- ------------------------------------------------------------
指定値              動作
none                入力不可になります。
text                文字
textCapCharacters   すべて大文字
textCapWords        単語の先頭を大文字
textCapSentences    文章の先頭を大文字
textAutoCorrect     文字のスペルミスを自動で修正する
textAutoComplete    文字の補完入力する
textMultiLine       文字を複数行入力する
textImeMultiLine    通常の文字入力時は複数行入力を許可せず、
                    IMEによって複数行入力を設定
textUri             URL
textEmailAddress    メールアドレス
textEmailSubject    メールの件名
textShortMessage    ショートメッセージ
textLongMessage     ロングメッセージ
textPersonName      人名
textPostalAddress   住所
textPassword        パスワード入力
textVisiblePassword パスワード入力(パスワードは表示)
textWebEditText     HTML
textFilter          他のデータでフィルタされた文字
textPhonetic        発音表記
number              数値入力
numberSigned        符号付きの数値
numberDecimal       小数入力
phone               電話番号
datetime            日付時刻
date                日付
time                時刻
------------------- ------------------------------------------------------------

参考:
Android Wiki* UIコンポーネント/TextView
http://wikiwiki.jp/android/?UI%A5%B3%A5%F3%A5%DD%A1%BC%A5%CD%A5%F3%A5%C8%2FTextView#inputType


▲ レイアウト

上記のうちのつぎの属性値を確認する。
    textPassword
    textEmailAddress
    textMultiLine
    date
    number

なお、 android:inputType 属性を指定していない場合もあわせて確認する。

□ 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" >

    <!-- (1) 通常のテキスト -->

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="文字列" >

        <requestFocus /> <!-- (7) -->
    </EditText>

    <!-- (2) パスワード -->

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="password"
        android:inputType="textPassword" />

    <!-- (3) email アドレス -->

    <EditText
        android:id="@+id/emailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email address"
        android:inputType="textEmailAddress" />

    <!-- (4) 複数行 -->

    <EditText
        android:id="@+id/multilineText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Multiline text"
        android:inputType="textMultiLine" />

    <!-- (5) 日付 -->

    <EditText
        android:id="@+id/date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="date"
        android:inputType="date" />

    <!-- (6) 数値 -->

    <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="number"
        android:inputType="number" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickHandler"
        android:text="@string/submit" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
---

▲ リソース

□ res/values/strings.xml
---
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">TextField01</string>
    <string name="submit">クリック</string>
</resources>
---


▲ アクティビティ

□ TextField01Activity.java
---
package jp.marunomaruno.android.textfield;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import jp.marunomaruno.android.textfield.R;

public class TextField01Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onClickHandler(View view) {
        EditText edit1 = (EditText) findViewById(R.id.editText1);
        EditText password = (EditText) findViewById(R.id.password);
        EditText emailAddress = (EditText) findViewById(R.id.emailAddress);
        EditText multilineText = (EditText) findViewById(R.id.multilineText);
        EditText date = (EditText) findViewById(R.id.date);
        EditText number = (EditText) findViewById(R.id.number);

        String text = String
                .format(" edit1=%s%n password=%s%n emailAddress=%s%n 
									multilineText=%s%n date=%s%n number=%s%n",
                        edit1.getText(), password.getText(), emailAddress
                                .getText(), multilineText.getText(), date
                                .getText(), number.getText());
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();

        TextView text1 = (TextView) findViewById(R.id.text1);
        text1.setText(text);
    }
}
---

△ EditText クラス

java.lang.Object 
   +  android.view.View 
     +  android.widget.TextView 
       +  android.widget.EditText 

ユーザーからの入力をサポートするためのクラス。
レイアウトでは、android:inputType 属性をつけることで、入力値を制限することができ
る。
特徴としては、getText() メソッドの返り値の型が Editable 型である。
String 型として、入力した値を取得するには、この返り値のオブジェクトの toString()
 メソッドを使って取得する必要がある。

・主なメソッド
---
Editable   getText() 
void       setText(CharSequence text, TextView.BufferType type)  

void       selectAll() 
void       setEllipsize(TextUtils.TruncateAt ellipsis) 
void       extendSelection(int index) 
void       setSelection(int index) 
void       setSelection(int start, int stop) 

void       setFilters(InputFilter[] filters)
final void setHint(CharSequence hint)
final void setHint(int resid)
void       setKeyListener(KeyListener input)
void       setHorizontallyScrolling(boolean whether)
void       setHeight(int pixels)
void       setWidth(int pixels)
---


△ Editable インターフェース

android.text.Editable

テキスト操作に関するインターフェース。
実際に使うためには、toString() メソッドで、テキストにしてから使う場合が多い。


★疑問
---
EditText の getText() はなぜ Editable 型で返すんだろうか? スーパークラスの 
TextView は CharSequence なのに。CharSequence は、String でも StringBuilder でも
いいので納得できるが。。
---


■ EditView で、最初からソフトウェアキーボードを表示しておく方法 (2012-03-09 追記)

レイアウトか Activity クラスで、目的の EditText にフォーカスを当てておく。
マニフェストの activity 要素で、android:windowSoftInputMode="stateVisible" 属性をつける。


□ レイアウト

---
    <EditText
        ... >
        <requestFocus /> <!-- <= これを指定 -->
    </EditText>
---


□ Activity クラス

---
    EditText editText = new EditText(this);
    editText.requestFocus();    // <= フォーカスを当てる(XML での指定でもよい)
---


□ AndroidManifest.xml

activity 要素に android:windowSoftInputMode 属性を追加

---
    <activity android:name=".XxxActivity"
              android:label="@string/app_name"
              android:windowSoftInputMode="stateVisible">  // <= これで表示される
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
---

                                                                        以上


最新の画像もっと見る

コメントを投稿