0

我有两个活动。EnterPin 和 ConfirmPin。数组是 pin 和 confirmpin。我需要比较两者。我想用一个文件来比较数组。Enterpin 将写入文件,并且 Confirmpin 应该读取并将其数组与文件进行比较。这可能吗?我一直在做一些研究,但似乎没有什么适合我想做的事情。

public class EnterPin extends Activity {
public int[] pin = new int[4];
public Button[] buttons = new Button[10];
public int counter = 0;
@Override
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);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_enter_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)
    {
        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)
   {
    TextView EnterPin = (TextView) findViewById(R.id.EnterPin);
    EnterPin.setText("Pin Entered");
    startActivity(new Intent("com.example.textsmslock.ConfirmPin"));
   }
}
public int GetPin(int pin)
{
    return pin;
}
}

.

public class ConfirmPin extends EnterPin {
public int[] pinconfirm = new int[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] = 9;
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}

public void ConfirmingPin()
{

    /*for(int i = 0; i < 4; i++)
    {
        if(pinconfirm[i] != pin[i])
        {
            TextView ConfirmPin = (TextView) findViewById(R.id.ConfirmPin);
            ConfirmPin.setText("Pins Do Not Match!!!!");


            startActivity(new Intent("com.example.textsmslock.ConfirmPin"));
        }*/
        if(pinconfirm.equals((pin))) == true)
        {
            TextView ConfirmPin = (TextView) findViewById(R.id.ConfirmPin);
            ConfirmPin.setText("Pin Confirmed");
            startActivity(new Intent("com.example.textsmslock.PinComfirmation"));
        }
        else
        {
            TextView ConfirmPin = (TextView) findViewById(R.id.ConfirmPin);
            ConfirmPin.setText("Pins Do Not Match!!!!");

            startActivity(new Intent("com.example.textsmslock.ConfirmPin"));

        }

}
}

这就是我最初尝试仅使用数组的方式

4

2 回答 2

0

对我来说,这听起来像 SharedPreferences 是你正在寻找的。它是一种用于存储数据的轻量级功能。

于 2012-11-20T21:57:06.307 回答
0

它需要是一个数组吗?

如果它只是一个字符串,你所需要的只是传递你的值:

//EnterPinActivity
Intent intent=new Intent(EnterPinActivity.this, ConfirmPinActivity.class);
intent.putExtra("pin", stringWithYourPinCode);
startActivity(intent);

//and then, in your ConfirmPinActivity
Intent intent=getIntent();
String yourPin=intent.getStringExtra("pin");

如果它绝对必须是一个数组,您仍然可以使用相同的putExtra()方法按照您的意图传递它。

我的观点是,通过将数据附加到 Intent 在两个活动之间传递数据比使用文件或 SharedPreferences(还包括文件 I/O 操作)更好(更快、更高效)。

于 2012-11-20T22:01:57.210 回答