2011-12-02 39 views
2

出於好奇,這是一個問題。Android:爲什麼我的EditText保持其價值,即使在方向改變?

我最近才知道,我必須關心保持自己的方向變化我的活動狀態。

但在我的佈局之一,Edittext只是保持其價值,我不明白爲什麼? 很明顯,我想重現此問題的其他EditTexts ...

繼承人的佈局中,EditText上是機器人: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> 

這裏是對的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; 
     } 
    });  
} 

這是包含不保留其狀態edittexts另一個活動的代碼

<?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(); 
    }   

} 
+0

你的意思是其他佈局中的其他EditTexts不保持其值?你可以從他們的活動中添加一些代碼嗎? –

+0

是的。他們不。我將在問題 – user1078083

回答

4

View S IN安卓做保留一些它的狀態。像EditText保留其文字,所以你不必保存它onSaveInstanceState。其他例子是ListView保持滾動位置。如果它是多行文本,EditText也會保持滾動位置。

但是一些用戶需要照顧的東西,如果你有一個筆記應用程序,你必須存儲正在編輯的筆記ID,以便在方向改變時你知道你在做什麼。

這是Android的標準方式,這裏沒什麼不尋常的。欲知詳情,請參閱。

Saving Activity State

關於你的活動不表現爲aspected一個是因爲代碼:

_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()); 

您應該看到它會打印輸入的值。

+0

中編輯另一個活動的代碼,重點在於其他EditText元素不保存輸入。我沒有看到任何可能導致這種行爲的差異 – user1078083

+0

所以你說,你有'EditText a',值爲''Hello「',當方向改變時它又是空的? – havexz

+0

我有一個活動A與佈局A,包含一個EditText。如果輸入「Hello」並更改方向,則保持「Hello」。我有另一個活動B與佈局B,包含一個EditText。如果輸入「Hello」並更改方向,則「Hello」已消失。兩者都發布在問題 – user1078083

0

如果你沒有給你的editText一個id,它不會保存在屏幕上的值的變化。即使屏幕改變方向,您也必須提供一個ID以保存值。

相關問題