平凡エンジニアからの出発

一に努力、二に理想、三に積小為大。

【AndroidTraining】EditText 1

Lesson

EditText 1

Mission

下記の仕様を満たすアプリを作りましょう。

  • ユーザーが入力した文字を画面に表示する

Snap Shot

f:id:atuyan39:20210406233257p:plainf:id:atuyan39:20210406233302p:plainf:id:atuyan39:20210406233308p:plain

Step by Step

  1. 新しくActivity(TrainingEditTextActivity.java)を追加する。
  2. xmlに、TextView, EditView, ButtonのViewを追加する。
  3. Viewを取得する
        // Viewを取得する
        TextView textView = findViewById(R.id.edit_text_1_text);
        EditText editText = findViewById(R.id.edit_text_1_edit_text);
        Button button = findViewById(R.id.edit_text_1_button);

4 . ボタンのクリックリスナーを設定する

        // Buttonのクリック時の処理を追加する
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              // 処理
            }
        });

5 . TextViewにEditTextに入力された値を設定する

                // TextViewにEditTextに入力された値を設定する
                textView.setText(editText.getText());

ラムダ式だと読みやすい

        // ※ラムダ式を使う場合
        // button.setOnClickListener(v -> textView.setText(editText.getText()));

Code

EditText 1 · atuyan39/AndroidTraining@ad0d836 · GitHub

Reference

EditText

EditText  |  Android Developers

属性(android:autofillHintsandroid:hint)を追加しないと注意される。

Memo

レイアウトは自由に。