기록
안드로이드 스튜디오 토글버튼 만들기 본문
화면단
<?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");
}
}
});
}
}
'JAVA' 카테고리의 다른 글
안드로이드 스튜디오 배수의합 구하기 (0) | 2023.01.02 |
---|---|
안드로이드 스튜디오 로또생성 (0) | 2022.12.30 |
안드로이드 스튜디오 전화번호 누르기 (0) | 2022.12.29 |
안드로이드 스튜디오 홀짝 맞추기 (0) | 2022.12.29 |
안드로이드 스튜디오 별찍기 (0) | 2022.12.29 |
Comments