c# - 如何让布局不可见

标签 c# android xamarin

我正在开发 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 ();

        };

在其他 Activity 中:

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

我该怎么做?

最佳答案

默认你的 View 可见性setVisibility(Gone) 然后。 首先,您每秒检查一次您的 sharedpreference 值是否不等于默认值,如果为真,则您的 View 可见性设置为 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();

关于c# - 如何让布局不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33519776/

相关文章:

c# - 有没有办法在没有数据源的情况下将 LINQ 查询创建为变量(尚未)?

android - 如何获取领域层的字符串资源

java - 为什么我从微调器中选择后无法跳转到选定的新 Activity ?

c# - xamarin.android - OnKeyLongPress 不触发

C# 如何显示像 : 这样的对象列表

c# - 2 在 C# 中的连续游戏

android - 如何使用 NetworkRequest.Builder().setNetworkSpecifier(string) 指定 wifi 网络

c# - 我可以在 Xaml 中使用 Device.OnPlatform 设置设备特定 View 吗?

c# - Xamarin "Attempting to JIT compile method "

c# - 将 JSON 字符串发送到 MVC Controller 失败