如何在 android eclipse 上的 tabhost 中实现共享首选项概念



我是安卓的自学者,

假设一个 android 程序将在文本视图上显示一些结果.任何人都可以告诉我如何在下一个屏幕上选项卡主机的第一个选项卡上显示该答案。如何实现这一点?

根据我的知识,我用谷歌搜索并发现"共享偏好"概念将有助于解决这个问题。我说的对吗?

我找到了一些样本,但它们并没有让我清楚,任何人都可以给我一些带有屏幕图像的例子。

感谢您的宝贵时间!

这里有一个小示例,无论您在第一个选项卡中输入什么,它都会显示在第二个选项卡上:

主类

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

新活动

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

第二活动

      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();
     }
   }
您必须在

第一个选项卡上将答案保存在共享首选项中,如下所示:

             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", "");

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>

接下来.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>

第二.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>

最新更新