2015-11-04 4 views
1

Я разрабатываю приложение для 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; 
    } 

Как я могу это сделать?

+0

Я не уверен, что вы просите. Вы хотите, чтобы у 'TextView' была установлена ​​'visibility'' '' '' до тех пор, пока вы не сможете получить текст из 'SharedPreferences' ...? – PPartisan

+0

Да, я хочу это @ PPartisan –

ответ

2

по умолчанию ваш вид видимость комплектVisibility (Gone) после этого. Сначала вы проверяете каждую секунду, что ваше значение sharedpreference не равно значению по умолчанию, если оно истинно, тогда видимость видимости задана видимость видимой. попробуйте этот код. Я не уверен, но я думаю, что этот код будет успешным.

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(); 
Смежные вопросы