简体   繁体   English

将问题Android小部件解析为int

[英]Parsing issue android widget to int

Need help parsing, I have tried "porting" my dice roller project to Android using Android Studio, I have most of the controller values replaced with their android widget counterparts, one problem, I am not sure how to properly parse widget values to an Int. 需要帮助解析,我尝试使用Android Studio将骰子滚子项目“移植”到Android,我将大多数控制器值替换为其对应的android小部件,这是一个问题,我不确定如何将小部件值正确解析为Int 。 I have marked them with aligned left comments below. 我在下面用对齐的左侧注释标记了它们。

modifier is an EditText 修饰符是一个EditText
result is a TextView 结果是一个TextView

I have tried many combinations and this is the most recent. 我已经尝试了许多组合,这是最新的组合。 The one that worked when it was pure java was .getValue().toString().trim() but I cannot use .getValue why is this? 当它是纯Java时,可以工作的是.getValue().toString().trim()但是我不能使用.getValue这是为什么?

public void onStart()
{
   super.onStart();
   percentile.setOnClickListener(new View.OnClickListener()
   {
     @Override
     public void onClick (View v)
     {
       {
          //issue is here
          int total = Nat20_core.roll10(cumulative.isChecked(),
                        Integer.parseInt (String.valueOf(modifier)),
                        Integer.parseInt(String.valueOf(result)));
          //end issue
          result.setText(String.valueOf(total));
        }
      }
    });
}

I have also tried this in a previous program as set 我也已经在以前的程序中尝试过

In EditText and TextView, the "value" is "text": 在EditText和TextView中,“值”是“文本”:

Integer.parseInt(modifier.getText().toString()) Integer.parseInt(modifier.getText()。toString())

This is because there is no .getValue() method for EditText and TextView widget. 这是因为EditText和TextView小部件没有.getValue()方法。

For EditText, you can use getText() which returns an Editable . 对于EditText,可以使用getText()返回一个Editable So, you need to get the string from it using toString() . 因此,您需要使用toString()从字符串中获取字符串。 So, you will need to use: 因此,您将需要使用:

modifier.getText().toString();

For TextView, you can use getText() which returns a CharSequence . 对于TextView,可以使用getText()返回一个CharSequence You also need to get the string from it using toString() . 您还需要使用toString()从字符串中获取字符串。 So, you can use the above line too: 因此,您也可以使用上面的行:

result.getText().toString();

Now, you need to convert the following code: 现在,您需要转换以下代码:

int total = Nat20_core.roll10(cumulative.isChecked(),
                        Integer.parseInt (String.valueOf(modifier)),
                        Integer.parseInt(String.valueOf(result)));

to: 至:

int total = Nat20_core.roll10(cumulative.isChecked(),
                        Integer.parseInt (modifier.getText().toString()),
                        Integer.parseInt(result.getText().toString()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM