【问题标题】:How to permanently remove a linearlayout?如何永久删除线性布局?
【发布时间】:2018-04-22 16:46:52
【问题描述】:

在这个程序中,我想在按下 mButton 时删除 ll2 (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>

【问题讨论】:

    标签: java android android-layout android-linearlayout


    【解决方案1】:

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

    【讨论】:

    • 谢谢!完美运行
    • 补充一点,如果您有时只显示一个视图,并且在某些操作后不想再显示它,您应该考虑使用 ViewStub 并将您的逻辑反转为“如果按钮没有还没有被点击,放大视图,否则什么都不做”。
    • 很高兴知道这有帮助!
    【解决方案2】:

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

    按下按钮后,立即移除 View (LinearLayout),将布尔值更改为 true 并将其保存到 SharedPreferences...

    在 onCreate() 中, 试试看

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

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      相关资源
      最近更新 更多