public class Password extends Activity{
Button one, two, three, four, five, six, seven, eight, nine, zero, exit, okay;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pass);
one = (Button) findViewById(R.id.button);
two = (Button) findViewById(R.id.button2);
three = (Button) findViewById(R.id.button3);
four = (Button) findViewById(R.id.button4);
five = (Button) findViewById(R.id.button5);
six = (Button) findViewById(R.id.button6);
seven = (Button) findViewById(R.id.button7);
eight = (Button) findViewById(R.id.button8);
nine = (Button) findViewById(R.id.button9);
exit = (Button) findViewById(R.id.button10);
zero = (Button) findViewById(R.id.button11);
okay = (Button) findViewById(R.id.button12);
}
public void onClick(View v){
switch (v.getId()){
case R.id.button:text.setText("1");
break;
case R.id.button2:text.setText("1");
break;
case R.id.button3:text.setText("1");
break;
case R.id.button4:text.setText("1");
break;
case R.id.button5:text.setText("1");
break;
case R.id.button6:text.setText("1");
break;
case R.id.button7:text.setText("1");
break;
case R.id.button8:text.setText("1");
break;
case R.id.button9:text.setText("1");
break;
case R.id.button10:text.setText("1");
break;
case R.id.button11:text.setText("1");
break;
case R.id.button12:text.setText("1");
break;
}
}
}
我在下面附上了图片。我想创建此 Activity 。我读了一些关于 sharedPreferences 的内容,但它仍然让我感到困惑。谁能告诉我如何实现这一点?
编辑:非常感谢大家。我做了一些其他的事情。开关盒不工作所以我添加了代码 one.setOnClickListener(){.......} 到每个按钮。它现在正在工作。 非常感谢你们所有人。如果我不在这里发帖,我永远不会有想法。
最佳答案
更新
试试这个
public void onClick(View v){
switch (v.getId()){
case R.id.button:text.setText(text.getText()+"1");
break;
case R.id.button2:text.setText(text.getText()+"2");
break;
case R.id.button3:text.setText(text.getText()+"3");
break;
case R.id.button4:text.setText(text.getText()+"4");
break;
case R.id.button5:text.setText(text.getText()+"5");
break;
case R.id.button6:text.setText(text.getText()+"6");
break;
case R.id.button7:text.setText(text.getText()+"7");
break;
case R.id.button8:text.setText(text.getText()+"8");
break;
case R.id.button9:text.setText(text.getText()+"9");
break;
case R.id.button10:text.setText("1");
break;
case R.id.button11:text.setText("1");
break;
case R.id.button12:text.setText("1");
break;
}
并为其他按钮放置适当的功能
用于编辑文本
android:inputType="number"
或
android:inputType="numberPassword"
在 xml 文件中的 edittext
SharedPreferences
引用 Waza_Be 的回答
关于android - 如何将密码 Activity 设置为仅使用数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17272426/