Android:为什么我的 EditText 保持其值(value),即使在方向改变时也是如此?

标签 android orientation android-edittext

这是出于好奇而提出的问题。

我最近刚刚了解到,我必须自己关心在方向改变时保持我的 Activity 状态。

但是在我的一个布局中,Edittext 只是保持其值,我不明白为什么? 显然我想为其他 EditTexts 重现这种行为......

这是布局,编辑文本是 android:id="@+id/createlistactivity_listname_edittext"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/title_bar"
        android:gravity="center"
        android:text="Enter a Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/createlistactivity_listname_edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:hint="Name..."
        android:singleLine="true" >

    </EditText>

</LinearLayout>


<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/CreateListActivity_HeaderLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/title_bar"
        android:gravity="center"
        android:text="Add Products to your new List"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice"
        android:footerDividersEnabled="true" >
    </ListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="No Products available. Insert some Products in the Products menu  first" />

</LinearLayout>


<LinearLayout
    android:orientation="horizontal"
    android:id="@+id/CreateListActivity_BottomBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    style="@android:style/ButtonBar" >


    <ImageButton
        android:id="@+id/CreateListActivity_SaveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:onClick="saveButtonClicked"
        android:src="@drawable/tick_32" />


    <ImageButton
        android:id="@+id/CreateListActivity_CancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:onClick="cancelButtonClicked"
        android:src="@drawable/block_32_2" />

</LinearLayout>

</LinearLayout>

这是 Activity 的onCreate

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.createlistactivity_layout);

    _selectedItems = new HashMap<Integer, Integer>();
    _productDAO = new ProductDAO(getApplication());
    _listDAO = new ListDAO(getApplication());
    _productCursor = _productDAO.getAllProductsCursor();

    startManagingCursor(_productCursor);

    setUpListView();    

    final EditText listNameEditText = (EditText)this.findViewById(R.id.createlistactivity_listname_edittext);

    listNameEditText.setOnEditorActionListener(new OnEditorActionListener() {

        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(listNameEditText
                        .getApplicationWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });     
}

这是另一个 Activity 的代码,其中包含不保留其状态的编辑文本

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


<TableLayout
    android:id="@+id/TableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/productImage"
    android:background="@android:drawable/alert_light_frame" >

    <TableRow
        android:id="@+id/productview_toprow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top" >

        <EditText
            android:id="@+id/productview_productname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="Product Name"
            android:singleLine="true" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/productview_pricelabel"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/black" />

        <EditText
            android:id="@+id/productview_price_edittext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="0.0"
            android:singleLine="true"
            android:inputType="numberDecimal" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/productview_unitlabel"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/black" />

        <EditText
            android:id="@+id/productview_unit_edittext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="kg|ml|..." />
    </TableRow>
</TableLayout>

和 onCreate...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.productview);

    _productName = (EditText)findViewById(R.id.productview_productname);
    _productPrice = (EditText)findViewById(R.id.productview_price_edittext);
    _productUnit = (EditText)findViewById(R.id.productview_unit_edittext);
    _productImage = (ImageButton)findViewById(R.id.productImage);

    _productDAO = new ProductDAO(getApplication()); 

    //set data from database
    _product = _productDAO.getProduct(getIntent().getExtras().getString(DataBaseKeys.PRODUCT_NAME));

    if(_product != null)
    {
        _productName.setText(_product.getName());
        _productImage.setImageBitmap(_product.getIcon());
        _productPrice.setText(""+_product.getPrice());
        _productUnit.setText(_product.getUnit());
        _productIcon = _product.getIcon();
    }           

}

最佳答案

好吧,android 中的View 会保留它的一些状态。像 EditText 一样,保留其文本,这样您就不必保存它 onSaveInstanceState。其他示例是 ListView 保持滚动位置。 EditText 如果是多行文本,也会保持滚动位置。

但是用户必须注意一些事情,比如如果您有一个 Note 应用程序,您必须存储正在编辑的 Note ID,以便在方向改变时您知道您在做什么。

这是Android的标准方式,没什么特别的。有关详细信息,请参阅。

Saving Activity State

关于您的一项 Activity 没有按方面表现是因为代码:

 _product = _productDAO.getProduct(getIntent().getExtras().getString(DataBaseKeys.PRODUCT_NAME));

if(_product != null)
{
    _productName.setText(_product.getName());
    _productImage.setImageBitmap(_product.getIcon());
    _productPrice.setText(""+_product.getPrice());
    _productUnit.setText(_product.getUnit());
    _productIcon = _product.getIcon();
}           

您正在覆盖编辑文本的值。在 if 语句之前尝试此日志语句。

Log.i("EDIT_NOT_WORKING", "VALUE OF EDIT: " + _productName.getText());

您应该看到它将打印输入的值。

关于Android:为什么我的 EditText 保持其值(value),即使在方向改变时也是如此?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8361501/

相关文章:

java - 来自 Youtube API 的视频无法在点击时播放

java - Android - 检查某些值不

java - 在Android中测量执行时间的最佳方法?

android - java.lang.ArrayIndexOutOfBoundsException while switch portrait and landscape (交错 GridView 库)

android - 如何在android中为平板电脑和手机设置不同的方向

android - 利用具有Android引擎的旧/新设备来驱动其他电子设备

CSS - 在横向和纵向位置之间切换

android - 无法从edittext获取数据,Android Kotlin

android - 编辑文本焦点更改监听器

java - 在不丢失多行功能的情况下为 EditText 设置输入类型?