Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

기록

안드로이드 스튜디오 토글버튼 만들기 본문

JAVA

안드로이드 스튜디오 토글버튼 만들기

9400 2022. 12. 29. 17:45

 

 

 

화면단 

<?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=".MainActivity">


    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:orientation="vertical"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp">

        <TextView
            android:id="@+id/et"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="Good Moring"
            android:textSize="40dp" />

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Click" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

package kr.co.aiai.app;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.et);
        tv.setText("Good evening");

        Button btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
//                Log.d("chiwon","hello");
                if(tv.getText() == "Good Morning"){
                    tv.setText("Good Evening");
                }else{
                    tv.setText("Good Morning");
                }
            }
          });
    }
}

 

Comments