我有两项活动。 EnterPin和ConfirmPin。阵列是pin和确认。我需要比较两者。我想用一个文件来比较数组。 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"));
}
}
}
这就是我最初尝试使用数组
的方法答案 0 :(得分:0)
对我来说,听起来好像是您正在寻找的SharedPreferences。它是一种用于存储数据的轻量级功能。
答案 1 :(得分: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操作)更好(更快,更有效)。