android - 如何将密码 Activity 设置为仅使用数字?

标签 android android-activity

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;
        }
    }
}

enter image description here

我在下面附上了图片。我想创建此 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/

相关文章:

android - 在android中实现多部分环的最佳方式

android - 聚类 - 如何处理具有相同位置的标记?

android - 最简单的 Android Activity 生命周期

android - 在 AsyncTask 完成时完成调用 Activity

android - 服务无法解析为类型

android - 对 NDK 中的不同库使用 ndk-build 和 cmake

iphone - 如何为 2D 游戏中的角色创建平滑路径

java - 如何以编程方式将 Activity 主题化为对话?

android - android Activity 生命周期函数的基础知识

android - 工具栏标题和选项在 fragment 上消失