ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Android] Button을 클릭하면 이름 띄우기
    BACK-END/Android 2020. 11. 11. 15:33

     

     

    MainActivity.java
    package com.example.calculator;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        Button bt;
        TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            bt = findViewById(R.id.button);
            tv = findViewById(R.id.tvMessage);
    
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(tv.getText().equals("박단비")){
                        tv.setText("TIS");
                    } else {
                        tv.setText("박단비");
                    }
                }
            });
        }
    }

     

    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">
    
        <TextView
            android:id="@+id/tvMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TIS"
            android:textSize="30pt"/>
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="버튼"
            android:textSize="30pt"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/"
            />
    </LinearLayout>

     

    출력 결과

     

     

     

    'BACK-END > Android' 카테고리의 다른 글

    [Android] weight 가중치  (0) 2020.11.12
    [Android] 새로운 애뮬레이터 만들기  (0) 2020.11.12
    [Android] Android SDK  (0) 2020.11.11
    [Android] Fragment 사용하기 2  (0) 2020.11.11
    [Android] Fragment 사용하기 1  (0) 2020.11.11

    댓글

Today
Designed by Danbee Park.