Android Studio中EditText及其ID的关系? [英] Relation of EditText with its Id in Android Studio?

查看:218
本文介绍了Android Studio中EditText及其ID的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请仔细阅读我的问题

我正在学习Activity LifeCycle .在具有某些id的XML布局中,我只有一个EditText和一个TextView.旋转屏幕时,似乎没有任何变化,因为旋转不会影响EditText .但是,当我删除XMLEditTextid并旋转屏幕时,旋转"开始生效,并且editText中的文本由于旋转而被删除.我对EditText与它的ID之间的关系感到困惑.

I am learning Activity LifeCycle. I only have one EditText and a TextView in my XML layout With certain id. When I rotate the screen, nothing seems to change because Rotation doesn't affect EditText. But when I remove the id of EditText in XML and rotated the screen, The Rotation Starts affecting and the text in the editText removed due to rotation. I am confused about the relation of EditText with its Id.

以下列名称的说明

  • 具有ID:EditText是否具有ID?
  • 旋转:电话是否旋转
  • EditText:旋转后EditText会发生什么情况.

.

Having Id ---------------- Rotation --------------- EditText

Having Id----------------Rotation---------------EditText

Yes                  Rotated            Does not change

No                   Rotated             Yes, Text Removed from editText

MainActivity.java为空,并且main_activity.xml代码如下:

MainActivity.java is empty and main_activity.xml code is below:


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

        <EditText

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter here"
            android:inputType="textPersonName"
            android:textSize="26sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"

            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.371" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="85dp"
            android:layout_height="25dp"
            android:layout_marginTop="88dp"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.496"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

对不起,由于我没有其他方式使用不好的英语和高分辨率图片.最好的问候

Sorry for using bad English and High-resolution pictures as I have no other ways. Best Regards

推荐答案

Android编程中最重要的方面之一是处理配置更改,其中最常见的是方向更改.尽管不建议自己处理配置更改,但是由于处理配置更改的隐患非常复杂,在某些情况下,我们可能需要手动处理.

One of the most important aspects in Android programming is to handle the configuration changes and most common of them is orientation changes. Although it is not recommended to handle configuration changes yourself, due to the hidden complexity of handling the configuration changes, there are cases where we might need to handle manually.

如果您需要保存状态,通常的方法是实施onSaveInstanceState来存储&恢复作为捆绑包传递的值.

If you need to save the state, the common approach is to implement onSaveInstanceState to store & restore the values which are passed as Bundle.

回到您的问题:

Relation of EditText with its Id in Android Studio?

当保存状态为View时,只要您在onSaveInstanceState中调用super方法,Android OS就会自动在视图层次结构中的每个视图上调用View.onSaveInstanceState方法.为了恢复数据,它使用android:id属性键表示该特定视图的状态.因此对于您而言,当您不为EditText提供id时,由于以下原因将不会存储该视图的状态缺少密钥.

When it comes to saving state of View Android OS automatically calls the View.onSaveInstanceState method on each of the views in your view hierarchy as long as you call through to the super method in onSaveInstanceState. In order to restore the data, it uses android:id attribute key for that particular View’s state.So for your case, when you don't provide the id for EditText, the View's state won't be stored due to missing key.

希望这可以阐明idEditText

这篇关于Android Studio中EditText及其ID的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆