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

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 {
/*
  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  本实例主要学习,屏幕翻转时,界面如何自适应,创建横屏布局
  1.禁止切换横屏:在 AndroidManifest.xml-->application->activity->中设置如下代码(android:screenOrientation="portrait")
   <activity android:name=".MainActivity" android:screenOrientation="portrait" >
  2. 创建 Landscape 布局,横屏时,会自动加载 Landscape 的布局界面(清单文件中,注意去掉 android:screenOrientation="portrait" )
  3. 翻转屏幕时,保存窗口控件的状态值;

  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 */
  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);

    //如果State中的值不为空,如果有相应的这个组件的值,则读取出来赋值上去
    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
  //将 textView 中的值,先保存到 outState 中(键值对)
  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". You can choose top, bottom, left, right, center, etc. center is equivalent to center_ vertical|center_ 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 on the button is capitalized in English 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" />

You can get the prompt text by setting the hint property, and set maxlines to make the maximum number of input lines in the input box.

If there are any omissions in the above related knowledge points, you can directly contact Xiaobian. Thank you for your reading and support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>