【问题标题】:How to Implement shared preference concept in tabhost on android eclipse如何在 android eclipse 上的 tabhost 中实现共享偏好概念
【发布时间】:2012-06-15 06:16:26
【问题描述】:

我是android的自学者,

假设一个 android 程序将在 textview 上显示一些结果。谁能告诉我如何在下一个屏幕上的选项卡主机的第一个选项卡上显示该答案。如何实现这一点?

据我所知,我在谷歌上搜索并发现“共享偏好”概念将有助于解决这个问题。我说的对吗?

我找到了一些示例,但它们并没有让我清楚,谁能给我一些带有屏幕图像的示例。

感谢您宝贵的时间!

【问题讨论】:

  • "我需要在下一页的选项卡主机的第一个选项卡上显示该答案"!!。你能改写一下吗?
  • 对不起我的朋友。我没有
  • 好吧...没关系。据我所知,我用谷歌搜索并发现“共享偏好”概念将有助于解决上述问题。我说的对吗?

标签: android eclipse android-tabhost


【解决方案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);

}
}

新活动

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

【讨论】:

  • 别误会,请将所有xml文件发给我,供我参考。你可以吗?
  • 当您在 NewActivity 中单击保存(选项卡优先)时,您在编辑文本中输入的值将保存在共享首选项中......然后在下一个活动中,即 SecondActivity(tsb 秒)中显示在文本视图中...
【解决方案2】:

您必须将您的答案保存在您的第一个标签上的共享偏好中,就像这样:

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

【讨论】:

  • 您能否给我一些关于使用共享偏好的 tabhost 的示例?
【解决方案3】:

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>

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 2019-06-20
    • 2012-06-05
    相关资源
    最近更新 更多