所以我有一个非常简单的应用程序,我正在努力.它的目的是从1台,1台或2台显示器收集资产数据.我的表单包含3个edittext视图和3个按钮(我为每个资产收集一个数据).该按钮调用startActivityForResult的条形码扫描仪,然后我想基于哪个按钮被按下(例如对结果传递给相关的EditText视图:按"扫描"按钮,"资产 - PC"的右边的EditText,扫描和返回数据如果您按下"资产 - Mon1"编辑文本旁边的"扫描"按钮,则将数据返回到"资产 - Mon1"编辑文本....依此类推......)
使用我在这里的代码,所有项目都可以正常运行.按下任何"扫描"按钮始终会将结果返回到第一个edittext视图"Asset - PC".
public class TestShit extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void assetPcClick(View view) {
Intent intent1 = new Intent("com.google.zxing.client.android.SCAN");
intent1.setPackage("com.google.zxing.client.android");
intent1.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent1, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetPC = …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用"SharedPreferences"来保留从多个edittext框保存/检索字符串值的功能,这样就可以了.我的活动中还有一个Spinner,它有一个字符串数组,用于它的可用值.但我不清楚如何将微调器选择写入SharedPreferences,然后读取SharedPreferences以退出并设置它的值.
这是我对edittext的配置:
-Button激活保存值到SharedPreferences-
public void buttonSaveSendClick(View view) {
SharedPreferences.Editor editor = getPreferences(0).edit();
EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
editor.putString("editTextCallIdtext", editTextCallId.getText().toString());
editor.putInt("selection-startCallId", editTextCallId.getSelectionStart());
editor.putInt("selection-endCallId", editTextCallId.getSelectionEnd());
editor.commit();
}
Run Code Online (Sandbox Code Playgroud)
-Button激活从SharedPreferences恢复上次保存的值 -
public void buttonRestoreLastClick(View view) {
SharedPreferences prefs = getPreferences(0);
EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
String editTextCallIdtextrestored = prefs.getString("editTextCallIdtext", null);
editTextCallId.setText(editTextCallIdtextrestored, EditText.BufferType.EDITABLE);
int selectionStartCallId = prefs.getInt("selection-startCallId", -1);
int selectionEndCallId = prefs.getInt("selection-endCallId", -1);
editTextCallId.setSelection(selectionStartCallId, selectionEndCallId);
}
Run Code Online (Sandbox Code Playgroud)
有关如何在第一个按钮(保存)中构建微调器选定值的集合的任何建议?那么如何在按下"恢复"按钮时将保存的值返回到微调器视图?