我正在尝试使用共享首选项存储引脚,因为我有两个不同的活动,即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?
答案 0 :(得分:1)
您正在使用错误类型的putString(String key, String value)
。
您尝试添加stringarray
而不是string
作为putString();
的密钥
同样作为同一putString();
的值,您还要添加int
,string
也应该editor.putString("PIN", pinconfirm[0].ToString());
。如果您不是pinconfirm中的第一项:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pinconfirm.length; i++) {
sb.append(pinconfirm[i]).append(",");
}
editor.putString("pinconfirm", sb.toString());
如果要添加数组中的所有内容,可以执行类似将其另存为单个逗号字符串的操作:
String[] pinconfirm= pinconfirms.split(",");
然后当您从SharedPreferences获取String时,只需解析它:
{{1}}