[英]How to permanently remove a linearlayout?
在這個程序中,我想刪除ll2
被按下時mButton(LinearLayout中)。 也就是說,我不希望這種布局在我第二次進入此活動時出現。 當我按下按鈕時,只要我在活動中,布局就消失了,但是當我回到活動中時,布局就在那里了。
如何永久刪除它? 提前致謝!
LinearLayout ll,ll2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
location_btn = (Button)findViewById(R.id.location_btn);
menu_btn = (Button)findViewById(R.id.bt_menu);
mButton = (Button) findViewById(R.id.buttone);
mEdit = (EditText) findViewById(R.id.edittexte);
ll2 = (LinearLayout)findViewById(R.id.llayout);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
number = mEdit.getText().toString();
mEdit.setText("");
ll2.setVisibility(View.GONE);
ll2.removeAllViewsInLayout();
}
});
}
我的布局文件
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/llayout"
android:visibility="visible">
<EditText
android:id="@+id/edittexte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SAVE"
android:id="@+id/buttone"/>
</LinearLayout>
您可以嘗試將布爾值保存在SharedPreferences
。例如,在開始時將布爾值保持為false
。
按下按鈕后,立即刪除View(LinearLayout),將布爾值更改為true
並將其保存到SharedPreferences ...
在onCreate()中,嘗試
if(booleanisTrue) {
ll2.setVisibility(View.GONE);
ll2.removeAllViewsInLayout();
}
只需保留一個SharedPreference
並保存您之前按下按鈕的狀態即可。 然后,每次您輸入活動時,請檢查存儲在您的SharedPreference
的值,如果發現之前已經按下過該按鈕,則只需隱藏LinearLayout
。
LinearLayout ll,ll2;
SharedPreference pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
pref = getSharedPreferences("MyApplication", Activity.MODE_PRIVATE);
location_btn = (Button)findViewById(R.id.location_btn);
menu_btn = (Button)findViewById(R.id.bt_menu);
mButton = (Button) findViewById(R.id.buttone);
mEdit = (EditText) findViewById(R.id.edittexte);
ll2 = (LinearLayout)findViewById(R.id.llayout);
// Check the preference value when activity is launched each time and hide of the button was pressed before
if(pref.getBoolean("ButtonPressed", false)) ll2.setVisibility(View.GONE);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
number = mEdit.getText().toString();
mEdit.setText("");
// Save the sate of the button pressed in the SharedPreference
pref.edit().putBoolean("ButtonPressed", true).apply();
ll2.setVisibility(View.GONE);
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.