res/new/android resource directory를 이용해

layout-land를 생성후 그 폴더안에 activity_main.xml을 생성

//가로방향으로 전환될때 사용하는 폴더

//onSaveInstanceState()를 호출하면서 전황시 상태값이 저장된다

activity_main.xml

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="vertical"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:layout_marginBottom="29dp"
android:ems="10"
android:hint="text"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="166dp"
android:layout_marginLeft="166dp"
android:layout_marginTop="29dp"
android:layout_marginEnd="157dp"
android:layout_marginRight="157dp"
android:text="check"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

activity_main.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="horizontal"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="text"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="242dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
tools:layout_editor_absoluteX="344dp"
tools:layout_editor_absoluteY="313dp"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {
String inputText;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayToast("onCreate() call");

//상태값 저장
editText = findViewById(R.id.editText);
button = findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
inputText= editText.getText().toString();
Toast.makeText(getApplicationContext(), "saved"+inputText, Toast.LENGTH_LONG).show();
} //onClick method를 overriding해준다
});
if(savedInstanceState != null){
inputText = savedInstanceState.getString("inputText");

Toast.makeText(getApplicationContext(), "saved value:" +inputText, Toast.LENGTH_LONG).show();
}
}

protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);

outState.putString("inputText", inputText);
}

// protected void onStart(){
// super.onStart();
// displayToast("onStart() call");
// }
protected void onStop(){
super.onStop();
displayToast("onStart() call");
}
protected void onDestroy(){
super.onDestroy();
displayToast("onDestroy() call");
}

public void displayToast(String message){
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

'android studio' 카테고리의 다른 글

alert dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (0) 2019.08.13
xml을 이용한 focus event  (0) 2019.08.12
touch event, gesture event  (0) 2019.08.12
카드뷰(CardView)  (0) 2019.08.12

+ Recent posts