我创建了这个简单的例子来说明我想要完成的事情。
这是我的第一个布局:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class LifeCycleActivity extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */
private RadioButton rbR, rbG, rbB;
private RadioGroup rg;
private Button next;
int color=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rbR = (RadioButton) findViewById(R.id.radio0);
rbB = (RadioButton) findViewById(R.id.radio1);
rbG = (RadioButton) findViewById(R.id.radio2);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
next = (Button) findViewById(R.id.button1);
final Intent it = new Intent(this, next.class);
final Bundle b = new Bundle();
rg.setOnCheckedChangeListener(this);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
b.putInt("color", color);
it.putExtras(b);
startActivity(it);
}
});
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId==rbR.getId()) color=1;
if (checkedId==rbB.getId()) color=2;
if (checkedId==rbG.getId()) color=3;
}
}
这是第二种布局:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
public class next extends Activity {
private LinearLayout ll;
private ImageView im;
private TextView tv;
private Button save;
private Bundle extras;
private int color=0;
private String selColor="";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
ll = (LinearLayout) findViewById(R.id.mainll);
ll.setOrientation(LinearLayout.VERTICAL);
im = new ImageView(this);
tv = new TextView(this);
save = new Button(this);
save.setText("save");
extras = getIntent().getExtras();
color = extras.getInt("color");
im.setImageResource(R.drawable.ic_launcher);
ll.addView(im);
if (color == 1) selColor = "RED";
if (color == 2) selColor = "BLUE";
if (color == 3) selColor = "GREEN";
tv.setText(selColor);
tv.setGravity(Gravity.CENTER);
ll.addView(tv);
ll.addView(save);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// here i want to save and exit
// so i can call onPause(), then finish()
// do not know how exactly since i have to follow some goals
// that i need for this example
}
});
}
}
我也有 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" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="red" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blue" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="green" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
和 next.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
如您所见,基于第一个布局中的颜色选择,我动态构建了第二个布局。
这就是我想做的,请帮助我(如果可能,添加代码):
- 在第二个布局上,如果我按保存,我想保存应用程序状态(无论我在该页面上构建什么)并退出。
- 如果我在该布局上添加一个按钮来创建其他动态对象(带有名称的EditText,或具有不同对象的另一个布局),而不是用户保存,他/她还将保存新对象(这部分可以稍后完成)
- 如果用户保存该页面一次,那么我希望他能够在退出之前获得与他/她相同的应用程序状态(当然,如果他/她选择相同的单选按钮)。
- 如果他/她选择另一个按钮,我不需要显示以前的状态,因为选择不同。