テキストボックスの作成
文字入力などが出来るテキストボックスを作成するにはEditTextを使います。
2行目のandroid:id="@+id/edittext2"がIDを指定する部分でedittext1というID名を設定しています。このID名を参照して入力されたテキストを取得したり書き換えたりするのでわかりやすい名前を付けます。
7行目のandroid:background="@drawable/frame_style1"が背景部分で、app->res->values->drawableのframe_style1.xmlで設定しています。
8行目のandroid:hint="@string/enter_digits"が何を入力するかヒントを表示する部分で@stringはapp->res->valuesのstring.xmlを表し、enter_digitsがstring.xmlのnameの部分になります。(<string name="enter_digits">数字を入力</string>)
9行目のandroid:inputType="numberDecimal"が入力制限をする部分でnumberDecimalは少数点を含めた数字を入力。
<activity_main.xml>
<EditText
android:id="@+id/edittext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/frame_style1"
android:hint="@string/enter_digits"
android:inputType="numberDecimal"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:textColor="@color/black"
android:textSize="18sp"/>
<drawable.xml>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#000000"/>
<solid
android:color="#00ffffff"/>
<corners android:radius="5dp"/>
</shape>
EditTextのinputTypeの入力制限。※制限は「|」で区切って複数指定可能。
種類 | 説明 |
---|---|
none | 編集不可 |
date | 日付 |
datetime | 日付と時刻 |
time | 時刻 |
number | 数字 |
numberPassword | 数字のパスワード |
numberDecimal | 少数点を含めた数字 |
numberSIgned | 符号付き数字 |
phone | 電話番号 |
textAutoCorrect | スペルチェック |
textComplete | 自動補完機能 |
textEmailAddress | メールアドレス |
textEmailSubject | メールタイトル |
textPersonName | 氏名 |
textPostalAddress | 住所 |
textPassword | パスワード |
textVisiblePassword | 可視化可能なパスワード |
textUri | URI |
textMultiLine | 複数行テキストエリア |
textImeMultiLine | IME入力版複数行テキストエリア |