【问题标题】:ParseInt SyntaxParseInt 语法
【发布时间】:2017-05-11 02:02:40
【问题描述】:

我在使用 Android sdk 中的 parseInt 方法时遇到问题。到目前为止,我的程序中有一个简单的编辑文本和一个按钮。我现在想在单击按钮时将数字保存在程序中的新变量中,那么我需要采取哪些步骤?

到目前为止,我的代码如下所示:

final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

    @override
    public void onClick(View v) {
        int val = Integer.parseInt(EditText.getText().toString());
    }
    // Perform action on click
});

【问题讨论】:

  • 我不明白你的问题。你到底想要什么?
  • 你的问题不清楚。

标签: java android parsing sdk int


【解决方案1】:

根据您的代码最简洁,

你可以像这样初始化 **EditText* 变量

final EditText editText= (EditText) findViewById(R.id.edittext);
final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int val = Integer.parseInt(editText.getText().toString());
        }
            // Perform action on click
        }

尝试使用上述解决方案,我希望它会工作

【讨论】:

    【解决方案2】:

    如果您想保存可从您的所有应用程序访问的变量,您可以使用共享首选项。

    例子:

    // 保存你的int

    final SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("App", Context.MODE_PRIVATE);
    final SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
    
    final EditText editText = (EditText) findViewById(R.id.edittext);
    final Button button = (Button) findViewById(R.id.button);
    
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int value = Integer.parseInt(editText.getText().toString());
    
            sharedPreferencesEditor.putInt("YourValue", value);
            sharedPreferencesEditor.commit();
        }
    });
    

    // 检索你的 int

    sharedPreferences.getInt("YourValue", 0);
    

    【讨论】:

    • 谢谢,它比我现在需要的复杂一点,但它也解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2022-07-27
    • 2017-01-01
    相关资源
    最近更新 更多