我在活动中有一些文本块,但是当我更改活动文本时,它会消失。
如何保存文字?
我知道它是Android中的共享首选项。但是如何使用它来保存活动中的文本?
代码
private void Display (){
LinearLayout display2 = FindViewById<LinearLayout> (Resource.Id.linearLayout12);
//LinearLayout display = FindViewById<LinearLayout> (Resource.Id.linearLayout13);
TextView productname = FindViewById<TextView> (Resource.Id.posttittle);
TextView price = FindViewById<TextView> (Resource.Id.price);
TextView weight = FindViewById<TextView> (Resource.Id.weight);
productname.Text = Intent.GetStringExtra ("title");
if (productname.Text == Intent.GetStringExtra ("title")) {
display2.Visibility = ViewStates.Visible;
}
else {
display2.Visibility = ViewStates.Gone;
}
price.Text = Intent.GetStringExtra("price");
weight.Text = Intent.GetStringExtra("weight");
//display2.Visibility = ViewStates.Visible;
productname.Visibility = ViewStates.Visible;
price.Visibility = ViewStates.Visible;
weight.Visibility = ViewStates.Visible;
}
我的Axml
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout12">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="147.6dp"
android:id="@+id/linearLayout13"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="276.2dp"
android:layout_height="match_parent"
android:id="@+id/linearLayout24">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="244.3dp"
android:layout_height="97.3dp"
android:id="@+id/posttittle"
android:layout_marginTop="22.4dp"
android:paddingTop="32dp"
android:layout_marginLeft="16.8dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="173.1dp"
android:layout_height="match_parent"
android:id="@+id/linearLayout25">
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="59.9dp"
android:layout_height="43.8dp"
android:id="@+id/weight"
android:layout_marginTop="54.6dp"
android:text="330u"
android:layout_marginLeft="5.6dp" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="82.3dp"
android:layout_height="42.4dp"
android:id="@+id/price"
android:text="88.00uhy" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="5dp"
android:id="@+id/linearLayout23"
android:background="#99999999" />
</LinearLayout>
答案 0 :(得分:0)
答案 1 :(得分:0)
如果要在活动之间保留文本值,可以使用onSaveInstanceState(Bundle savedInstanceState)
并将包中的值保存为名称 - 值对。
要从捆绑中获取值,您可以使用onRestoreInstanceState()
。
有关详细信息,请参阅this答案。
SharedPreferences
可用于在应用程序被杀死后保持值,onSaveInstanceState
适合维护活动之间的状态。