单击后退按钮恢复edittext字段

时间:2014-01-01 06:44:38

标签: android

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。任何人都可以指出错误

2 个答案:

答案 0 :(得分:1)

你做错了。错误的事情是

  1. 只是恢复您尝试重新启动整个活动的数据。
  2. 你自己调用onResume方法..
  3. 不要这样做。你可以通过以下方式轻松完成。

    1. 创建一个方法,为您的。
    2. 执行初始数据设置任务
    3. 在onResume of you方法(或在oncreate中)调用该方法
    4. 每当您想要重置数据时(即按下后退按钮后),请调用该方法。

答案 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视图,然后尝试将其值存储到喜好。