본문 바로가기

PROGRAM/Android

EditText, TextView, Button and Toast

Main UI

activity_main.xml

<?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">

    <EditText
        android:id="@+id/editText"
        android:layout_width="315dp"
        android:layout_height="45dp"
        android:ems="10"
        android:hint="Push Button"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="6dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="1dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java

TextView, button의 내용 입력

button.setText( "Clicked" )
editText.setText( "Button" )

EditText의 내용을 String 변수로 입력받기 위해서 사용하는 함수

editText.getText( ).toString( )

 

public class MainActivity extends AppCompatActivity {

    Button button;
    EditText editText;
    TextView textView;
    String toast_message;
    String edit_to_text;

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

        button = findViewById(R.id.button);
        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(button.getText().equals("Button")){
                    edit_to_text = editText.getText().toString();
                    button.setText("Clicked");
                    editText.setText("Clicked");

                    toast_message = "Clicked";
                }
                else{
                    edit_to_text = editText.getText().toString();
                    button.setText("Button");
                    editText.setText("Button");
                    toast_message = "go Back";
                }

                textView.setText(edit_to_text);
                Toast.makeText(getApplicationContext(),toast_message,Toast.LENGTH_SHORT).show();
            }
        });

    }
}

'PROGRAM > Android' 카테고리의 다른 글

MyCalc App(2)  (0) 2021.04.21
myCalc App (1)  (0) 2021.04.20
MyAlarm  (0) 2020.10.27
App 처음 만들기 - HelloAndroid  (0) 2020.10.21
Android Studio 4.1 Installation  (0) 2020.10.19