2

我正在尝试使用共享首选项存储 pin,因为我有两个不同的活动,即 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 吗?

4

1 回答 1

1

您正在使用putString(String key, String value)错误的类型。您正在尝试添加 astringarray而不是string作为Also 作为您添加putString(); 的相同值的键,这也应该是。如果您不想 pinconfirm 中的第一项:putString();intstring

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(",");
于 2012-11-26T13:54:23.600 回答