如何永久删除线性布局?

时间:2018-04-22 16:46:52

标签: java android android-layout android-linearlayout

在这个程序中,我想在按下mButton时删除itoa(str[i], b, 10);(LinearLayout)。即我第二次进入此活动时不希望出现此布局。当我按下按钮时,只要我在活动中,布局就会消失,但当我回到活动时,布局就在那里。

如何永久删除它?提前谢谢!

ll2

我的布局文件

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

2 个答案:

答案 0 :(得分:0)

您可以尝试在SharedPreferences中保存布尔值... 对于例如 将布尔值保留为false开头。

只要按下按钮,删除View(LinearLayout),将布尔值更改为true&将其保存到SharedPreferences ...

在onCreate()中, 试试

if(booleanisTrue) {
   ll2.setVisibility(View.GONE);
   ll2.removeAllViewsInLayout();
 }

答案 1 :(得分:0)

只需保留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);
        }
    });
}