Android新手。我有一个Activity,它创建了一些N个自定义视图。它创建的每个自定义视图都会添加到活动LinearLayout并显示在屏幕上。这可以正常工作,直到我旋转屏幕。突然,自定义视图无法正确显示其数据。在我的测试场景中,Activity创建了我的自定义视图的2个实例,第一个显示“Name 1”,而第二个在每个自定义视图的EditText中显示“Name 2”。当我切换方向时(这完全在我的Nexus 7上完成),两个自定义视图的EditText都说“名称2”。我在自定义视图的Activity和构造函数中使用了println语句,以确保仍然传入正确的值,但它只是以不同的方式显示。有任何想法吗?这是一些简化的代码:
public class EditTemplateWorkout extends Activity{
private int templateWorkoutId = -1;
private TemplateWorkout templateWorkout;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_template);
Intent intent = this.getIntent();
templateWorkoutId = intent.getIntExtra(Messages.TEMPLATEWORKOUTID_MESSAGE, -1);
templateWorkout = templateWorkoutId == -1 ? new TemplateWorkout() : LiftDroidDbHelper.TemplateWorkoutService.get(templateWorkoutId, true, this);
final EditText editTextName = (EditText) this.findViewById(R.id.editTextName);
editTextName.setText(templateWorkout.getName());
editTextName.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.equals("") ){
String txtVal = editTextName.getText().toString();
templateWorkout.setName(txtVal);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
LinearLayout root = (LinearLayout)this.findViewById(R.id.rootLayout);
root.removeAllViews();
for(TemplateExercise te : templateWorkout.getTemplateExercises()){
root = (LinearLayout)this.findViewById(R.id.rootLayout);
System.out.println("activity adding view for "+te.getName());
EditTemplateExerciseView editTemplateExercise = new EditTemplateExerciseView(te, this, null);
editTemplateExercise.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
root.addView(editTemplateExercise);
}
int moot = 3;
}
}
public class EditTemplateExerciseView extends LinearLayout {
TemplateExercise templateExercise;
public EditTemplateExerciseView(TemplateExercise te, Context context, AttributeSet attrs) {
super(context, attrs);
templateExercise = te;
setOrientation(LinearLayout.HORIZONTAL);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_edit_template_exercise, this, true);
TextView txtName = (TextView) this.findViewById(R.id.templateExerciseNameTxt);
txtName.setText(templateExercise.getName() + templateExercise.getTemplateExerciseID());
System.out.println("templateXercise id ="+te.getTemplateExerciseID());
System.out.println("set name:"+templateExercise.getName() + ", confirmed val="+txtName.getText());
}//constructor
}
以下是我简化的自定义视图的副本:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/templateExerciseNameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
</merge>
以下是截图的链接: http://imgur.com/a/2HqmS
以下是代码在呈现良好预期视图时的原始输出结果:
01-17 17:44:49.588: I/System.out(22474): activity adding view for Bench Press
01-17 17:44:49.618: I/System.out(22474): templateXercise id =4
01-17 17:44:49.618: I/System.out(22474): set name:Bench Press, confirmed val=Bench Press4
01-17 17:44:49.618: I/System.out(22474): activity adding view for Incline Bench
01-17 17:44:49.648: I/System.out(22474): templateXercise id =6
01-17 17:44:49.648: I/System.out(22474): set name:Incline Bench, confirmed val=Incline Bench6
当屏幕方向改变并调用Activity的onCreate方法时,以下是代码的println输出结果:
01-17 17:45:31.568: I/System.out(22474): activity adding view for Bench Press
01-17 17:45:31.588: I/System.out(22474): templateXercise id =4
01-17 17:45:31.588: I/System.out(22474): set name:Bench Press, confirmed val=Bench Press4
01-17 17:45:31.588: I/System.out(22474): activity adding view for Incline Bench
01-17 17:45:31.618: I/System.out(22474): templateXercise id =6
01-17 17:45:31.618: I/System.out(22474): set name:Incline Bench, confirmed val=Incline Bench6
01-17 17:45:31.648: W/IInputConnectionWrapper(22474): showStatusIcon on inactive InputConnection
01-17 17:45:31.808: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:31.878: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:31.978: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:32.178: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:32.318: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
01-17 17:45:32.338: W/IInputConnectionWrapper(22474): getTextBeforeCursor on inactive InputConnection
您可以看到我将预期值传递到第一个自定义视图,但它显示了第二个自定义视图的值。您在此处看到的代码与最初的视图相比大大简化了。任何机器大师都知道我在做错了什么?
答案 0 :(得分:0)
你需要它保存你的CustomViews状态,当你改变设备的方向时,android OS从头开始初始化你的视图,所以尝试在你的视图方法中实现
onSaveInstanceState
将在方向更改之前由onpause方法调用,此处您将从edittext
保存数据和
onRestoreInstanceState
将在完成定位后调用,并将正确的数据添加到您的视图
这里是完整代码:
How to prevent custom views from losing state across screen orientation changes