Android 从纵向更改为横向以及从横向更改为纵向时,取消编辑文本的内容

Android 从纵向更改为横向以及从横向更改为纵向时,取消编辑文本的内容,android,android-layout,android-edittext,android-orientation,Android,Android Layout,Android Edittext,Android Orientation,在我的应用程序中,布局是按照以下代码用java创建的: frameLayout = new FrameLayout(this); frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); HorizontalScrollView HSC = new HorizontalScrollView(this); HSC.setLayou

在我的应用程序中,布局是按照以下代码用java创建的:

frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

HorizontalScrollView HSC = new HorizontalScrollView(this);
HSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
frameLayout.setBackgroundResource(R.drawable.lavagna1);       
ScrollView VSC = new ScrollView(this);
VSC.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  

inflater = (LayoutInflater) SecondaAttivitaEQ.this.getSystemService(LAYOUT_INFLATER_SERVICE);

layout = inflater.inflate(R.layout.keyboardmatrix,(ViewGroup) findViewById(R.id.ciao));

    for ( int i = 0; i < grado; i++) {
        tableRow = new TableRow(this); 
        tableRow.setGravity(Gravity.CENTER);

        for (int j = 0; j < 1; j++) {

            valore[i][j].setHint(" c" + i + " ");
            valore[i][j].setPadding(10, 10, 10, 10);
            tableRow.addView(valore[i][j]);
            tableLayout.addView(tableRow);

        }
     }

    VSC.addView(tableLayout);
    HSC.addView(VSC);
    frameLayout.addView(HSC);
    setContentView(frameLayout);

    secondo = new Button (this);
    secondo.setText("SOLVE");
    secondo.setTextColor(0xffffffff);
    secondo.setBackgroundResource(R.drawable.but_ok);
    tableLayout.addView(secondo);
frameLayout=新的frameLayout(此);
frameLayout.setLayoutParams(新的frameLayout.LayoutParams(LayoutParams.MATCH_父级,LayoutParams.MATCH_父级));
HorizontalScrollView HSC=新的HorizontalScrollView(此);
HSC.setLayoutParams(新的FrameLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容,Gravity.CENTER));
frameLayout.setBackgroundResource(R.drawable.lavagna1);
ScrollView VSC=新的ScrollView(此);
setLayoutParams(新的FrameLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
TableLayout TableLayout=新的TableLayout(本);
tableLayout.setLayoutParams(新的FrameLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
充气器=(布局充气器)第二个ATTIVITAEQ.this.getSystemService(布局充气器服务);
布局=充气机。充气(R.layout.keyboardmatrix,(视图组)findViewById(R.id.ciao));
对于(int i=0;i
这段代码创建了一个EditText表,问题在于屏幕的旋转,事实上如果我输入值​​在EditText中,当手机旋转时,应用程序通过删除EditText的所有内容来重新创建布局。
你能帮我解决这个问题吗?

每次方向改变时,android都会创建新视图并销毁旧视图。可以在方向更改时保存数据,并在创建新视图时重新初始化

使用活动的
onConfiguration Changed
方法检测方向更改

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
  }
像这样更新
AndroidManifest.XML
以包含android:configChanges

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">