我是android的自学者,
假设一个android程序将在textview上显示一些结果。任何人告诉我如何在下一个屏幕上的标签主机的第一个标签上显示该答案。如何实现这一目标?
根据我的知识,我用Google搜索并发现“共享偏好”概念将有助于解决此问题。我是对的吗?
我找到了一些样品,但他们没有让我清楚,任何人都可以给我一些屏幕图像的例子。
感谢您宝贵的时间!
答案 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();
}
}
答案 1 :(得分: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", "");
答案 2 :(得分: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>