Detailed explanation of saving code of Android Studio screen direction and UI interface state

  • 2021-11-13 18:13:52
  • OfStack

Project: Orientation


package com.example.orientation;
 
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
/*
  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
   This example mainly learns how to adapt the interface and create a horizontal screen layout when the screen is flipped 
  1. Prohibit switching horizontal screen: in  AndroidManifest.xml-->application->activity-> Set the following code in ( android:screenOrientation="portrait" ) 
   <activity android:name=".MainActivity" android:screenOrientation="portrait" >
  2.  Create  Landscape  Layout, horizontal screen, will automatically load  Landscape  The layout interface of (in the manifest file, pay attention to remove it  android:screenOrientation="portrait"  ) 
  3.  Save the state value of the window control when flipping the screen ;
 
  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 */
  Button button;
  TextView textView;
 
  String TAG = "myTag";
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    button = findViewById(R.id.button );
    textView = findViewById(R.id.textView);
 
    // If State The value in is not empty. If there is a corresponding value of this component, read it out and assign it 
    if(savedInstanceState !=null)
    {
      String s = savedInstanceState.getString("key");
      textView.setText(s);
    }
 
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        textView.setText(button.getText());
      }
    });
  }
 
  @Override
  protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG,"onDestroy:");
  }
 
  @Override
  // Will  textView  The value in, first save to the value in  outState  Medium (key-value pair) 
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("key",textView.getText().toString());
  }
}

Extended learning:

UI Interface Design

TextView


<TextView
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="This is a TextView"
    android:textColor="#00ff00"
    android:textSize="24sp" />

To center the text, you need to add the attribute android: gravity= "center", and the options you can choose are top, bottom, left, right, center, etc. center is equivalent to center_verticalcenter_horizontal.
Use android: textSize= "24sp" to specify the text size and android: textColor= "# 00ff00" to specify the text color.

Button


<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:textAllCaps="false"/>

In Android, the text above Button is all capitalized by default, which can be changed by setting android: textAllCaps = "false"

EditText


<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="HelloWorld"
    android:maxLength="20"
    android:maxLines="1" />

Prompt text can be obtained by setting hint property, and setting maxLines makes the maximum number of input lines in the input box.

If there are any omissions in the above related knowledge points, you can contact this site directly. Thank you for reading and supporting this site.


Related articles: