此例子通过结合Spinner和Intent的使用来说明,具体的代码如下:
TestActivity.java如下所示:
package com.my.test;
import com.my.test00.Test00;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TestActivity extends Activity {
private TextView tv;
private Button b;
SharedPreferences sp;
String color;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button);
tv=(TextView)findViewById(R.id.tv);
//Bundle bundle=this.getIntent().getExtras();
//color=bundle.getString(Test00.COLOR);
sp=getSharedPreferences(Test00.TAG,0);
color=sp.getString(Test00.COLOR, "绿色");
if(color=="绿色"){
tv.setTextColor(Color.GREEN);
}
else if(color=="红色"){
tv.setTextColor(Color.RED);
}
else if(color=="黄色"){
tv.setTextColor(Color.YELLOW);
}
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(TestActivity.this,Test00.class);
startActivity(i);
finish();
System.gc();
}
});
}
}
Test00.java如下所示:
import com.my.test.R;
import com.my.test.TestActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class Test00 extends Activity{
SharedPreferences sp;
private Spinner s;
private ArrayAdapter<String> aa;
private String color;
private Button btn;
public static final String TAG="tag";
public static final String COLOR="color";
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test00);
btn=(Button)findViewById(R.id.btn);
s=(Spinner)findViewById(R.id.spinner);
aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
aa.add("绿色");
aa.add("红色");
aa.add("黄色");
s.setAdapter(aa);
s.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
s.setSelection(arg2);
color=s.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sp=getSharedPreferences(Test00.TAG,0);
SharedPreferences.Editor ed=sp.edit();
ed.putString(COLOR, color);
ed.commit();
/**
* 这里不能简单地使用一个finish()来返回TestActivity
* 而不使用Intent,因为这样没有传值给TestActiviy.java
* */
Intent i=new Intent(Test00.this,TestActivity.class);
startActivity(i);
finish();
System.gc();
/*Intent i=new Intent(Test00.this,TestActivity.class);
Bundle b=new Bundle();
b.putString(Test00.COLOR, color);
i.putExtras(b);
startActivity(i);*/
}
});
}
}
main.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:text="设置"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:text="测试性文字!测试性文字!"
android:textSize="35dip"
/>
</LinearLayout>
test00.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" >
<Button
android:id="@+id/btn"
android:text="保存设置并返回"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
AndroidManifest.xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.my.test00.Test00"></activity>
</application>
</manifest>