public class MainActivity extends Activity implements SurfaceHolder.Callback {
EditText , PatientInfo,PatientAge,PatientId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
Button buttonStopCameraPreview = (Button)findViewById(R.id.stopcamerapreview);
Button buttonCapturePreview = (Button) findViewById(R.id.Capturecamerapreview);
rgGender = (RadioGroup) findViewById(R.id.rgGender);
rdbMale = (RadioButton) findViewById(R.id.rdbMale);
rdbFemale = (RadioButton) findViewById(R.id.rdbFemale);
PatientInfo = (EditText) findViewById(R.id.PatientName);
PatientInfo.setHint("enter patient name");
PatientAge = (EditText) findViewById(R.id.Age);
PatientAge.setHint("Age");
PatientId = (EditText) findViewById(R.id.PatientId);
PatientId.setHint("PatientId");
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this)
rawCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
Log.d("Log", "onPictureTaken - raw");
}
};
shutterCallback = new ShutterCallback()
{
public void onShutter() {
Log.i("Log", "onShutter'd");
}
};
jpegCallback = new PictureCallback()
{
public void onPictureTaken(byte[] data, Camera camera)
{
loadInput();
int imageNum = 0;
String Name = PatientInfo.getText().toString();
String Age = PatientAge.getText().toString();
String Id = PatientId.getText().toString();
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Date d = new Date();
CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
Rname = s.toString() +".jpg";
File imagesFolders = new File(Environment.getExternalStorageDirectory().toString() + "/" + Name + Age + gender + Id);
imagesFolders.mkdirs();
File output = new File(imagesFolders, Rname);
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(MainActivity.this,
"Image saved: ",
Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{}
Log.d("Log", "onPictureTaken - jpeg");
}
buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
{
........
}
buttonCapturePreview.setOnClickListener(new OnClickListener()
{...}
buttonStopCameraPreview.setOnClickListener(new Button.OnClickListener(){
......}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId())
{
case R.id.rdbMale:
if (checked)
gender= "M";
gender1="M";
rdbFemale.setChecked(false);
break;
case R.id.rdbFemale:
if (checked)
gender = "F";
gender1="F";
rdbMale.setChecked(false);
break;
}
}
Button backButton = (Button) findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
Intent intent = new Intent(MainActivity.this, MainActivity.class);
saveInput();
startActivity(intent);
}
});
}
protected void saveInput()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putString("Name",PatientInfo.getText().toString()).commit();
preferences.edit().putString("Age", PatientAge.getText().toString()).commit();
preferences.edit().putString("Id",PatientId.getText().toString()).commit();
}
private void loadInput(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String savedTextN = preferences.getString("Name", null);
String savedTextA = preferences.getString("Age", null);
String savedTextI = preferences.getString("Id", null);
PatientInfo.setText(savedTextN);
PatientAge.setText(savedTextA);
PatientId.setText(savedTextI);
System.out.println(savedTextN);
System.out.println(savedTextA);
System.out.println(savedTextI);
}
我正在尝试加载我在第一个活动中输入的相同数据。在按钮上单击数据是lodt。所以我使用了共享偏好。但我第一次使用它,所以我的代码有问题。我只是尝试加载其中一个edittext。任何人都可以指出错误
答案 0 :(得分:1)
你做错了。错误的事情是
不要这样做。你可以通过以下方式轻松完成。
答案 1 :(得分:0)
您错误地使用错误密钥从首选项中获取数据。
当您使用Name
方法和onResume
方法中的密钥loadInput
将偏好值存储在首选项中时,您尝试使用PatientInfo
密钥获取该值这在您的偏好中不可用。
更改您的loadInput
方法,如下所示:
private void loadInput(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String savedText = preferences.getString("Name");
PatientInfo.setText(savedText);
}
由于onResume
方法始终在onCreate
之前调用,因此您需要在PatientInfo
方法中初始化onResume
视图,然后尝试将其值存储到喜好。