目次
1. エディットテキストとは何ですか?
EditText は、ユーザーのテキスト入力を受け取るために使用できる Android のテキスト入力コントロールです。以下は、EditText コントロールの一般的に使用されるパラメーター プロパティの詳細な説明です。
-
android:id
: コントロールの一意の識別子。レイアウト ファイルおよびコード内でコントロールを参照するために使用されます。 -
android:layout_width
: コントロールの幅。値は、特定の値 ( など20dp
)、match_parent
(親コンテナを満たす幅)、またはwrap_content
(コンテンツに基づいた適応幅) です。 -
android:layout_height
: コントロールの高さ。値は、特定の値 ( など20dp
)、match_parent
(親コンテナを満たす高さ)、またはwrap_content
(コンテンツに基づいた適応高さ) です。 -
android:text
: EditText に表示されるテキストの内容。 -
android:hint
: EditText が空の場合に表示されるプロンプト テキスト。 -
android:textColor
: EditText のテキストの色。 -
android:textSize
: EditText のテキストのサイズ。 -
android:inputType
: 入力タイプ。ユーザーによるコンテンツ入力を制限するために使用されます。一般的な値は次のとおりです。text
:プレーンテキスト入力。number
:数値入力。phone
:電話番号を入力します。email
:メール入力。password
:パスワードを入力すると文字が非表示になります。
-
android:maxLines
: EditText が表示できる最大行数。 -
android:maxLength
: ユーザーが入力する最大文字数を制限します。 -
android:imeOptions
: ボタンの表示方法の決定など、入力方法 (ソフト キーボード) 関連の動作オプションを定義します。 -
android:singleLine
: テキストを 1 行で表示するかどうか。 -
android:enabled
: EditText コントロールを有効にするかどうか。false に設定すると、EditText は編集できなくなります。 -
android:editable
: 編集可能かどうか。非推奨です。代わりに使用してくださいandroid:enabled
。 -
android:gravity
top
: テキストの配置。 、bottom
、left
、right
、center_vertical
、およびその他の値を組み合わせることができますcenter_horizontal
。 -
android:background
: EditText の背景を設定します。これは、色の値または画像リソースにすることができます。
上記は、EditText コントロールの一般的に使用されるパラメーター プロパティであり、さまざまなユーザー入力のニーズやインターフェイス設計要件を満たすために必要に応じて構成できます。
2. 利用手順
1.デモ
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DictionaryTableActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="26dp"
tools:layout_editor_absoluteY="0dp">
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="button"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/edit_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="username"
android:hint="输入内容"
tools:ignore="MissingConstraints" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
public class DictionaryTableActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary_table);
Button button = (Button) findViewById(R.id.button_2);
editText = (EditText) findViewById(R.id.edit_1);
button.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_2:
String inputString = editText.getText().toString();
Toast.makeText(DictionaryTableActivity.this, inputString,Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}