帮助我找到解决方案。下面是我的代码,它扫描并在文本视图中显示,现在我想以任何格式保存此文本视图。
以下是我的代码。
public class MainActivity extends AppCompatActivity {
TextView tv;
Button bt;
String contents="", format="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.contents);
bt = (Button)findViewById(R.id.save);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent in) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, in);
if (scanningResult != null) {
try {
contents = in.getStringExtra("SCAN_RESULT");
format = in.getStringExtra("SCAN_RESULT_FORMAT");
tv.setText("Content-" + contents + "\n" + " Format-" + format);
}
catch (NullPointerException e){}
}
}
public void save(View v){
if(contents.equals("")){
Toast.makeText(getApplication(),"Click Camera icon to Scan BarCode",Toast.LENGTH_LONG).show();
}
else{
//search scan result on web
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(contents)));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_camera:
IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
scanIntegrator.initiateScan();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
我如何使用共享偏好来实现这一目标?
答案 0 :(得分:0)
要将值保存到SharedPrefence
,您可以执行以下操作:
private static final String PREF_KEY_SOMETHING = "PREF_KEY_SOMETHING";
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString(PREF_KEY_SOMETHING, yourValue).apply();
要再次检索它,请执行以下操作。
String yourValueBack = sharedPreferences.getString(PREF_KEY_SOMETHING ,null);