[Android Studio] セクション 8 EdiText コントロール

目次

1. エディットテキストとは何ですか?

2. 利用手順

1.デモ



1. エディットテキストとは何ですか?

EditText は、ユーザーのテキスト入力を受け取るために使用できる Android のテキスト入力コントロールです。以下は、EditText コントロールの一般的に使用されるパラメーター プロパティの詳細な説明です。

  1. android:id: コントロールの一意の識別子。レイアウト ファイルおよびコード内でコントロールを参照するために使用されます。

  2. android:layout_width: コントロールの幅。値は、特定の値 ( など20dp)、match_parent(親コンテナを満たす幅)、またはwrap_content(コンテンツに基づいた適応幅) です。

  3. android:layout_height: コントロールの高さ。値は、特定の値 ( など20dp)、match_parent(親コンテナを満たす高さ)、またはwrap_content(コンテンツに基づいた適応高さ) です。

  4. android:text: EditText に表示されるテキストの内容。

  5. android:hint: EditText が空の場合に表示されるプロンプト テキスト。

  6. android:textColor: EditText のテキストの色。

  7. android:textSize: EditText のテキストのサイズ。

  8. android:inputType: 入力タイプ。ユーザーによるコンテンツ入力を制限するために使用されます。一般的な値は次のとおりです。

    • text:プレーンテキスト入力。
    • number:数値入力。
    • phone:電話番号を入力します。
    • email:メール入力。
    • password:パスワードを入力すると文字が非表示になります。
  9. android:maxLines: EditText が表示できる最大行数。

  10. android:maxLength: ユーザーが入力する最大文字数を制限します。

  11. android:imeOptions: ボタンの表示方法の決定など、入力方法 (ソフト キーボード) 関連の動作オプションを定義します。

  12. android:singleLine: テキストを 1 行で表示するかどうか。

  13. android:enabled: EditText コントロールを有効にするかどうか。false に設定すると、EditText は編集できなくなります。

  14. android:editable: 編集可能かどうか。非推奨です。代わりに使用してくださいandroid:enabled

  15. android:gravitytop: テキストの配置。 、bottomleftrightcenter_vertical、およびその他の値を組み合わせることができますcenter_horizontal

  16. 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;
        }
    }

}

Supongo que te gusta

Origin blog.csdn.net/AA2534193348/article/details/131467428
Recomendado
Clasificación