0

我正在尝试将 editText 保存到 Sharedprefrences ,以便它显示在 TextView 中amnt。它显示它但不保存它?我究竟做错了什么?我是新手,只是周末的一个小爱好,所以如果这是一个愚蠢的问题,我很抱歉,谢谢你的时间。

package org.iimed.www;

import java.util.Calendar;

import org.iimed.www.DateTimePicker.DateWatcher;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.inputmethodservice.Keyboard.Key;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class CaAdd extends Activity implements DateWatcher,OnClickListener{
Button button_click,amount;
int amountt = 0;
Key at;
EditText amountedit;
 String amountText; 
 String amountString;
TextView dateResult, date2,amnt;
int hrs,min;
SharedPreferences.Editor editor ;
SharedPreferences prefs;
String amntprefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetimeactivity);
dateResult = (TextView)findViewById(R.id.textview1);
date2 = (TextView)findViewById(R.id.textView2);
 button_click = (Button)findViewById(R.id.setbtn);

button_click.setOnClickListener(this);
amnt = (TextView)findViewById(R.id.amountText);


}
public  final String AT = "Amount"; 




public void loadDefaultSharedPreferences() { 
    prefs = PreferenceManager.getDefaultSharedPreferences(this);


                if (prefs != null) {
                    String loadUsername= prefs.getString(
                            AT , null);
                    if (loadUsername != null && !loadUsername.isEmpty()) {
                        amnt.setText(loadUsername);
                    }}}

@Override
public void onClick(View v) {
    switch (v.getId()){


    case R.id.setbtn:




final Dialog mDateTimeDialog = new Dialog(this);
// Inflate the root layout
final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null);
// Grab widget instance
final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
mDateTimePicker.setDateChangedListener(this);
mDateTimePicker.initData();
amountedit = (EditText)mDateTimeDialogView.findViewById(R.id.amountedit);
ImageButton increase = (ImageButton)mDateTimeDialogView.findViewById(R.id.increase);
increase.setOnClickListener(new OnClickListener(){
    public void onClick(View v){


amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);


    }
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){

    public void onClick(View v){

      amountt--;
      amountText = Integer.toString(amountt);
      amountedit.setHint(amountText);
      amnt.setText(amountText);


    }
});

Button setDateBtn = (Button)mDateTimeDialogView.findViewById(R.id.SetDateTime);
// Update demo TextViews when the "OK" button is clicked
setDateBtn.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
            Intent intent=new Intent(CaAdd.this,AlarmReceiver.class);
            PendingIntent pi=PendingIntent.getActivity(CaAdd.this, 2,    intent,PendingIntent.FLAG_CANCEL_CURRENT);
           AlarmManager alm=(AlarmManager) getSystemService(Context.ALARM_SERVICE);




            hrs=mDateTimePicker.getHour();
            min=mDateTimePicker.getMinute();
            Calendar calendar=Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,hrs);
            calendar.set(Calendar.MINUTE, min);
            calendar.set(Calendar.SECOND, 0);
            alm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), pi);

         mDateTimePicker.clearFocus();
          String result_string = mDateTimePicker.getMonth() + "/" + String.valueOf(mDateTimePicker.getDay()) + "/" + String.valueOf(mDateTimePicker.getYear())
         + "  " + String.valueOf(mDateTimePicker.getHour()) + ":" + String.valueOf(mDateTimePicker.getMinute());


          if(mDateTimePicker.getHour() > 12) result_string = result_string + "PM";
         else result_string = result_string + "AM";
         date2.setText(result_string);


         mDateTimeDialog.dismiss();
         }
         });




Button cancelBtn = (Button)mDateTimeDialogView.findViewById(R.id.CancelDialog);
// Cancel the dialog when the "Cancel" button is clicked
cancelBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
mDateTimePicker.reset();
mDateTimeDialog.cancel();
}
});

// Reset Date and Time pickers when the "Reset" button is clicked

Button resetBtn = (Button)mDateTimeDialogView.findViewById(R.id.ResetDateTime);
resetBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
mDateTimePicker.reset();
}
});

mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Set the dialog content view
mDateTimeDialog.setContentView(mDateTimeDialogView);
//Display the dialog
mDateTimeDialog.show();
}}



public void onDateChanged(Calendar c) {
Log.e("","" + c.get(Calendar.MONTH) + " " + c.get(Calendar.DAY_OF_MONTH)+ " " + c.get(Calendar.YEAR));
String result_string =String.valueOf(c.get(Calendar.MONTH)+1)  + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" + String.valueOf(c.get(Calendar.YEAR));
// _tvUserDOB.setTypeface(CGlobalVariables.tfFoEnglish);
dateResult.setText(result_string+" (mm/dd/yyyy)");


   }

public void saveDefaultPreferences() {
     SharedPreferences  prefs = PreferenceManager.getDefaultSharedPreferences(this);{
    SharedPreferences.Editor editor = prefs.edit();
    final String AT = "Amount";



     editor.putString(AT,amountText);
    editor.commit();}
}}
4

2 回答 2

0

共享首选项允许您保存和检索键、值对数据。

初始化:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
// 0 - for private mode
Editor editor = pref.edit();

存储数据:

editor.putString("key_name", "string value"); // Storing string
editor.commit(); // commit changes

检索数据:

pref.getString("key_name", null); // getting String

要了解有关 SharedPreferences 的更多信息或尝试一个简单的示例,请查看

于 2013-11-13T06:16:58.300 回答
0

您只能为某些原语和字符串(也为字符串集)保存键值对。Android 视图永远无法保存在任何地方。

SharedPreferences manager =PreferenceManager.getDefaultSharedPreferences(context);

使用它来保存和检索数据,如下所示

// for saving data
manager.edit().putString(context.getString(AT), YOUR_TEXT).commit()

// for retrieving data
manager.getString(context.getString(AT), "");
于 2013-11-13T06:18:44.900 回答