我试图使用共享的首选项存储大头针,因为我有两个不同的活动,即EnterPin和ConfirmPin。
对于EnterPin,我有
[起始代码]
package com.example.textsmslock;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class EnterPin extends Activity {
public String[] pin = new String[4];
public Button[] buttons = new Button[10];
public int counter = 0;
public String FILENAME = "pin_file";
public static final String PinCode = "1234";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_pin);
//getActionBar().setDisplayHomeAsUpEnabled(true);
buttons[1] = (Button)findViewById(R.id.button1);
buttons[2] = (Button)findViewById(R.id.button2);
buttons[3] = (Button)findViewById(R.id.button3);
buttons[4] = (Button)findViewById(R.id.button4);
buttons[5] = (Button)findViewById(R.id.button5);
buttons[6] = (Button)findViewById(R.id.button6);
buttons[7] = (Button)findViewById(R.id.button7);
buttons[8] = (Button)findViewById(R.id.button8);
buttons[9] = (Button)findViewById(R.id.button9);
buttons[0] = (Button)findViewById(R.id.button0);
//Restore the preferences
SharedPreferences settings = getSharedPreferences(PinCode, 0);
}
public void button1(View view)
{
if(counter < 3)
{
pin[counter] = "1";
counter++;
}
else
{
PinEntered();
}
}
public void button2(View view)
{
if(counter < 3)
{
pin[counter] = "2";
counter++;
}
else
{
PinEntered();
}
}
public void button3(View view)
{
if(counter < 3)
{
pin[counter] = "3";
counter++;
}
else
{
PinEntered();
}
}
public void button4(View view)
{
if(counter < 3)
{
pin[counter] = "4";
counter++;
}
else
{
PinEntered();
}
}
public void button5(View view)
{
if(counter < 3)
{
pin[counter] = "5";
counter++;
}
else
{
PinEntered();
}
}
public void button6(View view)
{
if(counter < 3)
{
pin[counter] = "6";
counter++;
}
else
{
PinEntered();
}
}
public void button7(View view)
{
if(counter < 3)
{
pin[counter] = "7";
counter++;
}
else
{
PinEntered();
}
}
public void button8(View view)
{
if(counter < 3)
{
pin[counter] = "8";
counter++;
}
else
{
PinEntered();
}
}
public void button9(View view)
{
if(counter < 3)
{
pin[counter] = "9";
counter++;
}
else
{
PinEntered();
}
}
public void button0(View view)
{
if(counter < 3)
{
pin[counter] = "0";
counter++;
}
else
{
PinEntered();
}
}
public void PinEntered()
{
if(counter == 3)
{
putString(pin,0);
}
}
}
[结束码]
对于我的ConfirmPin
[起始代码]
package com.example.textsmslock;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class ConfirmPin extends EnterPin {
public String[] pinconfirm = new String[4];
public Button[] buttons = new Button[10];
public int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm_pin);
//getActionBar().setDisplayHomeAsUpEnabled(true);
buttons[1] = (Button)findViewById(R.id.button1);
buttons[2] = (Button)findViewById(R.id.button2);
buttons[3] = (Button)findViewById(R.id.button3);
buttons[4] = (Button)findViewById(R.id.button4);
buttons[5] = (Button)findViewById(R.id.button5);
buttons[6] = (Button)findViewById(R.id.button6);
buttons[7] = (Button)findViewById(R.id.button7);
buttons[8] = (Button)findViewById(R.id.button8);
buttons[9] = (Button)findViewById(R.id.button9);
buttons[0] = (Button)findViewById(R.id.button0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_confirm_pin, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void button1(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "1";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button2(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "2";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button3(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "3";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button4(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "4";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button5(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "5";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button6(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "6";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button7(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "7";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button8(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "8";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button9(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "9";
counter++;
}
else
{
ConfirmingPin();
}
}
public void button0(View view)
{
if(counter < 3)
{
pinconfirm[counter] = "0";
counter++;
}
else
{
ConfirmingPin();
}
}
public void ConfirmingPin()
{
SharedPreferences settings = getSharedPreferences(PinCode, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(pinconfirm, 0);
editor.commit();
}
}
我在调用putString时遇到错误,有人可以向我解释原因吗?我没有正确地执行sharedPreferences吗?
最佳答案
您使用的putString(String key, String value)
类型错误。
您正在尝试添加stringarray
而不是string
作为putString();
的键
另外,作为同一putString();
的值,您还要添加一个int
,该值也应为string
。如果您不想在确认中输入第一项:
editor.putString("PIN", pinconfirm[0].ToString());
如果要添加数组中的所有内容,则可以执行类似将其保存为单个逗号分隔字符串的操作:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pinconfirm.length; i++) {
sb.append(pinconfirm[i]).append(",");
}
editor.putString("pinconfirm", sb.toString());
然后,当您从SharedPreferences获取字符串时,只需像下面这样解析它:
String[] pinconfirm= pinconfirms.split(",");