2012-06-15 91 views
2

共享偏好的概念,我的自我學習到Android,如何實現在tabhost Android上的日食

假設一個Android程序將顯示在textview.can一些結果沒有任何人告訴我如何展示這個問題的答案在標籤主機的第一個選項卡上的下一個屏幕上。如何實現這一目標?

根據我的知識,我搜索並發現「共享偏好」概念將有助於解決這個問題。我是對的嗎?

我發現了一些樣本,但他們沒有讓我清楚,任何人都可以給我一些屏幕圖像的例子。

感謝您寶貴的時間!

+0

「我需要在下一頁的標籤主機的第一個標籤上顯示該答案」!!你可以重溫嗎? –

+0

對不起,我的朋友。我沒有任何 –

+0

好吧...它的好。根據我的知識,我搜索並發現「共享偏好」的概念將有助於上述問題。我是對的嗎? –

回答

1

這裏是一個小在此示例中,無論您在第一個標籤中輸入什麼,它都顯示在第二個標籤上:

主類

public class CheckkActivity extends TabActivity { 
/** Called when the activity is first created. */ 
    @Override  
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Resources res = getResources();     
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 


    intent = new Intent().setClass(this, NewActivity.class); 


    spec = tabHost.newTabSpec("first").setIndicator("First") 
        .setContent(intent); 
    tabHost.addTab(spec); 


    intent = new Intent().setClass(this, SecondActivity.class); 
    spec = tabHost.newTabSpec("second").setIndicator("Second") 
        .setContent(intent); 
    tabHost.addTab(spec); 



    tabHost.setCurrentTab(0); 

} 
} 

NewActivity

 public class NewActivity extends Activity{ 
    EditText get; 
    Button save; 
    SharedPreferences sharedPreferences; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.next); 
     get=(EditText)findViewById(R.id.next); 
     save=(Button)findViewById(R.id.button1); 


      save.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(get.getText().toString().equalsIgnoreCase("")){ 
        Toast.makeText(getApplicationContext(), "enter something", Toast.LENGTH_SHORT).show(); 
       }else{ 
       sharedPreferences=PreferenceManager.getDefaultSharedPreferences(NewActivity.this); 
        Editor editor1 = sharedPreferences.edit(); 
        editor1.remove("answer"); 
        editor1.commit(); 
       sharedPreferences=PreferenceManager.getDefaultSharedPreferences(NewActivity.this); 
        Editor editor = sharedPreferences.edit(); 
        Log.i("set value",""+get.getText().toString()); 
        editor.putString("answer",get.getText().toString()); 
        editor.commit();} 
      } 
     }); 

    } 

    } 

SecondActivity

 public class SecondActivity extends Activity{ 
TextView set; 
SharedPreferences sharedPreferences; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second); 
    set=(TextView)findViewById(R.id.second); 

} 

    @Override 
    protected void onResume() { 
// TODO Auto-generated method stub 
sharedPreferences = PreferenceManager 
     .getDefaultSharedPreferences(SecondActivity.this); 
String answer= sharedPreferences.getString("answer", ""); 
Log.v("get value",""+answer); 
if(answer.equalsIgnoreCase(null)){ 
    set.setText("nothing to display"); 
}else{ 
set.setText(answer); 
} 
super.onResume(); 
    } 
    } 
+0

不要誤會我,請將所有xml文件發給我參考。你可以嗎? –

+0

當你點擊保存在NewActivity(標籤第一)時,那麼你在edittext中輸入的值被保存在共享偏好....然後在下一個活動,即SecondActivity(TSB秒)它顯示在一個文本視圖中... – Anu

0

你必須保存你的答案共享偏好您的第一個標籤,就像這樣:

   SharedPreferences sharedPreferences; 
      sharedPreferences=PreferenceManager.getDefaultSharedPreferences(YourActivityName.this); 
      Editor editor = sharedPreferences.edit(); 
      editor.putString("your_tag",your value); 
      editor.commit(); 

然後在接下來的活動剛剛獲取的值:

   SharedPreferences sharedPreferences; 
      sharedPreferences = PreferenceManager 
     .getDefaultSharedPreferences(NextActivity.this); 
    String answer= sharedPreferences.getString("your_tag", ""); 
+0

你可以給我一些關於tabhost使用共享偏好的例子嗎? –

0

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="5dp"> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp" /> 
    </LinearLayout> 
    </TabHost> 

next.xml

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<EditText 
    android:id="@+id/next" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="save" /> 

</LinearLayout> 

second.xml

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/second" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout>