如何将TextView(id)作为双精度数返回?

huangapple go评论78阅读模式
英文:

How can i return TextView(id) as double?

问题

TextView 布局

    mTextValue = (TextView) findViewById(R.id.amount2);

要在另一个私有函数中实现类似以下的操作

    double amount = Double.parseDouble(mTextValue.getText().toString());

XML

    <TextView
        android:id="@+id/amount2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:hint="金额"
        android:inputType="number"
        android:text="15"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        app:layout_constraintStart_toEndOf="@+id/Total"
        app:layout_constraintTop_toTopOf="@+id/Total" />

我想要获取这个 TextView 并将其作为 double 使用然后返回 (return amount2 * 100)
英文:

TextView layout

mTextValue = (TextView) findViewById(R.id.amount2);

To be able to get like this in another private void:

double amount= mTextValue;

XML

&lt;TextView
    android:id=&quot;@+id/amount2&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;16dp&quot;
    android:hint=&quot;amount&quot;
    android:inputType=&quot;number&quot;
    android:text=&quot;15&quot;
    android:textColor=&quot;@android:color/black&quot;
    android:textSize=&quot;24sp&quot;
    app:layout_constraintStart_toEndOf=&quot;@+id/Total&quot;
    app:layout_constraintTop_toTopOf=&quot;@+id/Total&quot; /&gt;

I want to get the TextView and use it as double and return to be (return amount2 * 100);

答案1

得分: 1

你可以将TextView的值作为String获取。您需要将其解析为双精度浮点数。

Double.valueOf(mTextValue.getText());

您需要确保TextView中的文本是双精度浮点数,否则此方法会抛出异常。

英文:

You get the value of TextView as String. You need to parse it into double.

Double.valueOf(mTextValue.getText());

You need to make sure the text in the TextView is a double, otherwise this method throws an exception.

答案2

得分: 1

代替使用TextView,使用EditText来从用户获取输入:

<EditText
    android:id="@+id/amount2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:hint="amount"
    android:inputType="number"
    android:text="15"
    android:textColor="@android:color/black"
    app:layout_constraintStart_toEndOf="@+id/Total"
    app:layout_constraintTop_toTopOf="@+id/Total" />

然后使用以下代码来获取用户输入的值:

final EditText amountEt = findViewById(R.id.amount2); // 查找编辑框
final String userInput = amountEt.getText().toString(); // 获取编辑框中输入的值
double amount;

if (!userInput.isEmpty()) {
    try {
        amount = Double.parseDouble(userInput);

        // 在这里进行你的计算和其他操作

    } catch (Exception e) {
        e.printStackTrace();
    }
} else {
    // 显示空输入提示信息
}
英文:

Instead of using TextView use EditText to take input from a user:

&lt;EditText
    android:id=&quot;@+id/amount2&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;16dp&quot;
    android:hint=&quot;amount&quot;
    android:inputType=&quot;number&quot;
    android:text=&quot;15&quot;
    android:textColor=&quot;@android:color/black&quot;
    app:layout_constraintStart_toEndOf=&quot;@+id/Total&quot;
    app:layout_constraintTop_toTopOf=&quot;@+id/Total&quot; /&gt;

And then to get the user input value use:

    final EditText amountEt = findViewById(R.id.amount2); // find the edit text
    final String userInput = amountEt.getText().toString(); // get the edittext value entered
final double amount;

if(!userInput.isEmpty()) {
  try {
    amount = Double.parseDouble(userInput);

    // do your calculations here and other stuffs

  } catch (Exception e) {
    e.printStackTrace();
  }
} else {
    // show empty input message
  }

huangapple
  • 本文由 发表于 2020年9月24日 23:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64049545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定