ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Android] 버튼눌렀을때 Text 변하게 하기
    App/Android 2023. 12. 17. 23:17

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <!-- note
        orientation : LinearLayout에서 세로로 텍스트나열
        -->
    
    
        <EditText
            android:id="@+id/et_id"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:hint="아이디를 입력하세요"/>
    
        <Button
            android:id="@+id/btn_test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="버튼"/>
    
    </LinearLayout>

     

     

    MainActivity.java

    package com.example.study_1;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        EditText et_id;
        Button btn_test;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
    
            et_id = findViewById(R.id.et_id);
            btn_test = findViewById(R.id.btn_test);
    
            btn_test.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    et_id.setText("LYJ");
                }
            });
    
        }
    }

     

     

     

     

     

    댓글

Designed by Tistory.