gpt4 book ai didi

java - 保存安卓 Activity 状态

转载 作者:行者123 更新时间:2023-11-30 10:09:25 25 4
gpt4 key购买 nike

我刚学Android编程,遇到一个问题。

我使用以编程方式添加的复选框小部件创建 Activity ,或者如果用户触摸添加按钮 (tambah) 将添加复选框小部件,问题是如何保存状态 Activity ?

主 Activity .java

public class MainActivity extends Activity {

// Variable Global
int checkId = 0; //CheckBox Id
EditText ex;
TextView noText;
LinearLayout m;
CheckBox check;
CheckBox noCheck;
String dat;
Toast errorNot;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void funcOne(View view) {

/**
* tambah.onClick function
* @param ex - EditText variable
* @param noText - TextView variable used for spacing
* @param m - CheckBox main layout
* @param check - Generated CheckBox widget
* @param noCheck - Toggle between CheckBox and EditText
* @param dat - EditText variable converted to String
* @param errorNot - To display noData error
*/

ex = (EditText)findViewById(R.id.editData);
noText = new TextView(this);
m = (LinearLayout)findViewById(R.id.checkBoxLayout);
check = new CheckBox(this);
noCheck = (CheckBox)findViewById(R.id.noCheck);
dat = ex.getText().toString();
errorNot = Toast.makeText(this, "No input data", Toast.LENGTH_LONG);

// Method
if (dat.length() > 1) {
if (noCheck.isChecked()) {
noText.setText(dat);
m.addView(noText);
} else {

/**
* @param n - New Toast (Only for debugging)
*/

checkId ++;
Toast n = Toast.makeText(this, Integer.toString(checkId), Toast.LENGTH_SHORT);
check.setTag("ch"+checkId);
check.setText(dat + " <WidgetTag " +check.getTag() + ">");
m.addView(check);
n.show();
}
} else {
errorNot.show();
}
}

public void addSpace(View view) {

/**
* space.onClick function
* @param b - Child layout
* @param d - TextView
*/

LinearLayout b = (LinearLayout)findViewById(R.id.checkBoxLayout);
TextView d = new TextView(this);
b.addView(d);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//outState.putBoolean("AstringKey", noCheck);
outState.putString("AStringKey2", dat);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//savedInstanceState.getBoolean("AStringKey");
savedInstanceState.getString("AStringKey2");
}

应用布局: http://imgur.com/gallery/1ZfJ5QL

最佳答案

每个 Activity 中的 onCreate() 方法都有一个 Bundle 参数。您可以在其中保存实例。您可以在 onPause() 中编写代码,以便在完成 Activity 之前它会存储您的内容。您可以使用相同的包再次访问它

关于java - 保存安卓 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53276831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com