我怎样才能在android中使用分享首选项?

yquo 发布于 2019-08-14 android 最后更新 2019-08-14 08:39 1 浏览

对不起,因为我的简单问题3,但我需要你的帮助。我想在我的应用程序中有一个计数器(使用shared preference),如下所示:

  • 首先,有2个按钮,STARTRESET。如果RESET是 点击,计数器从0开始。 另外,如果点击START,计数器将从共享首选项数据开始。
  • 开始计数
  • 最后一次,我想在股票偏好中保存柜台。 (但我不知道最好将它保存在BACK btn或CLICK btn中)
我的问题是在共享偏好部分。请帮我,我该怎么做?非常感谢!
编辑:这是我的代码
public class CountActivity extends Activity {
private Button click;
private int count,savedCount;
private String count_text;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.counting);
click= (Button) findViewById(R.id.vow_counting);
    final Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/Far_Homa.ttf");
final SharedPreferences sharedPreferences=getSharedPreferences("counters", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor=sharedPreferences.edit();
AlertDialog.Builder fBuilder=new AlertDialog.Builder(VowCountActivity.this);
fBuilder.setMessage("please choose");
    fBuilder.setCancelable(false);
fBuilder.setPositiveButton("start from beging", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            count = sharedPreferences.getInt("counter", 0);
click.setText("0");
            click.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/Far_Homa.ttf"));
            dialogInterface.cancel();
        }
    });
fBuilder.setNegativeButton("countinue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            count = sharedPreferences.getInt("counter",savedCount);
editor.putInt("counter",savedCount).commit();
            dialogInterface.cancel();
        }
    });
fBuilder.show();
click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           count++;
           count_text=Integer.toString(count);
           click.setText(count_text);
           click.setTypeface(typeface);
           savedCount = sharedPreferences.getInt("savedCounter", count);
           vibrate(500);
        }
    });
}
// vibrate
public void vibrate(int duration)  {
    Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibs.vibrate(duration);
}
已邀请:

fet

赞同来自:

尝试这个:

SharedPreferences preferences = getSharedPreferences("counterStat", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = preferences.edit();
btnStart.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                int counter = preferences.getInt("counter", 0);
             // counter = counter + 1;  start counting  
                edit.putInt("counter", counter).commit();
               }
             });
btnReset.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {   
              edit.putInt("counter", 0).commit();
             }
            });
对话回:
  int counter = preferences.getInt("counter", 0);
  edit.putInt("savedCounter", counter).commit();
对话取消:
 int counter = preferences.getInt("counter", 0);
 edit.putInt("savedCounter", counter).commit();
 edit.putInt("counter", 0).commit();
然后你可以使用:int savedCounter = preferences.getInt(“savedCounter”,0);随地

wnatus

赞同来自:

使用

        SharedPreferences preferences = getSharedPreferences("PROFILE", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("yourPreferenceName", yourPreferenceObject);
        edit.commit();
为了保存偏好 和
       SharedPreferences preferences = this.getSharedPreferences("PROFILE", Context.MODE_PRIVATE);
       String string = preferences.getString("yourPreferenceName", "defaultReturn");
用于接收您的sharedPreferences .get和.put返回类型

wquod

赞同来自:

要获得开始按钮的值,您可以定义此功能并具有共享首选项的值

public static int getIntPreferences(String key) {
    SharedPreferences settings = context.getSharedPreferences(SP_FILE_NAME, 0);
    return settings.getInt(key, 0);
}
现在要重置共享首选项值,您可以使用以下功能
public static void updatePreferences(String key, int value) {
    SharedPreferences settings = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt(key, value);
    editor.commit();
}

dut

赞同来自:

Here is how to add an entry to SharedPrefs, and save to disk.
SharedPreferences settings = getSharedPreferences("my_prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", "Adam");
editor.commit();
Here is how to read a saved value from SharedPrefs.
SharedPreferences settings = getSharedPreferences("my_prefs", 0);
String name = settings.getString("name", null);

kquis

赞同来自:

你应该使用这个代码,istear of yourrsL

public class CountActivity extends Activity {
private Button click;
private int count,savedCount;
private String count_text;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.counting);
click= (Button) findViewById(R.id.vow_counting);
    final Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/Far_Homa.ttf");
final SharedPreferences sharedPreferences=getSharedPreferences("counters", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor=sharedPreferences.edit();
AlertDialog.Builder fBuilder=new AlertDialog.Builder(VowCountActivity.this);
fBuilder.setMessage("choose");
    fBuilder.setCancelable(false);
fBuilder.setPositiveButton(firstا", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            count =0;
            savedCount=0;
            click.setText("0");
            click.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/Far_Homa.ttf"));
            dialogInterface.cancel();
        }
    });
fBuilder.setNegativeButton("countinue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            count = sharedPreferences.getInt("counter",savedCount);
            count_text=Integer.toString(count);
            click.setText(count_text);
            click.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Far_Homa.ttf"));
            dialogInterface.cancel();
        }
    });
fBuilder.show();
click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           count++;
           savedCount=count;
           count_text=Integer.toString(count);
           click.setText(count_text);
           click.setTypeface(typeface);
           editor.putInt("counter",savedCount).commit();
           savedCount = sharedPreferences.getInt("savedCounter", count);
}
    });
}