如何使布局不可见

本文关键字:布局 何使 | 更新日期: 2023-09-27 18:20:27

我正在开发一个Android应用程序。

我有一些存储在SharedPreferences中的属性。

我需要布局-直到存储在共享首选项中的值没有文本。

存储值时,我需要使布局可见。

zakazat.Click += delegate {
            var intent = new Intent (this, typeof(CartActivity));
            intent.PutExtra ("title", (string)(firstitem ["post_title"]));
            intent.PutExtra ("price", (string)(firstitem ["price"] + " грн"));
            intent.PutExtra ("weight", (string)(firstitem ["weight"] + "г"));
            StartActivity (intent);
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
            ISharedPreferencesEditor editor = prefs.Edit ();
            editor.PutString ("title", (string)(firstitem ["post_title"]));
            editor.PutString ("price", (string)(firstitem ["price"] + " грн"));
            editor.PutString ("weight", (string)(firstitem ["weight"] + "г"));
           editor.Apply ();
        };

在其他活动中:

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");
        price.Text = Intent.GetStringExtra("price");
        weight.Text = Intent.GetStringExtra("weight");

        display2.Visibility = ViewStates.Visible;
        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
        string product = prefs.GetString ("title", "");
        string _price = prefs.GetString ("price", "");
        string _weight = prefs.GetString ("weight", "");
            productname.Text = product;
        price.Text = _price;
            weight.Text = _weight;



        //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;
    }

我该怎么做?

如何使布局不可见

默认情况下,您的视图可见性setVisibility(Gone)之后。首先,每隔一秒钟检查一次sharedpreference值是否不等于默认值。如果为true,则将视图可见性设置为visibility visible试试这个代码我不确定,但我认为这个代码会成功。

Thread t = new Thread() {
  @Override
  public void run() {
    try {
      while (!isInterrupted()) {
        Thread.sleep(1000);
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
       SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String value = shared.getString("key","Default");
if(!value.equalsto("Default"){
view.setVisibility(View.VISIBLE);
}
          }
        });
      }
    } catch (InterruptedException e) {
    }
  }
};
t.start();