2

我是android的自学者,

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

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

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

感谢您宝贵的时间!

4

3 回答 3

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();
     }
   }
于 2012-06-15T07:17:58.437 回答
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", "");
于 2012-06-15T06:25:02.877 回答
0

主要的.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>
于 2012-06-15T07:39:19.300 回答