Android - 将双重传递给TextView?

时间:2016-02-19 00:55:53

标签: java android android-edittext

所以我正在开发一款应用来为我计算力量。它工作正常,直到我尝试将最终的数字值传递给TextView。这是代码(calc_Click是单击按钮时调用的内容):

package com.hoodeddeath.physicscalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class GravityActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gravity);
    }

    public void calc_Click(View v){
        double TEN = 10;
        double NEGELEVEN = -11;
        double G = 6.67*(Math.pow(TEN,NEGELEVEN));
        double TWO = 2;
        String mOneText = ((EditText)findViewById(R.id.massOne)).getText().toString();
        String aOneText = ((EditText)findViewById(R.id.ampOne)).getText().toString();
        String mTwoText = ((EditText)findViewById(R.id.massTwo)).getText().toString();
        String aTwoText = ((EditText)findViewById(R.id.ampTwo)).getText().toString();
        String distText = ((EditText)findViewById(R.id.distance)).getText().toString();
        String aThreeText = ((EditText)findViewById(R.id.ampThree)).getText().toString();

        double mOne = Double.parseDouble(mOneText);
        double aOne = Double.parseDouble(aOneText);
        double mTwo = Double.parseDouble(mTwoText);
        double aTwo = Double.parseDouble(aTwoText);
        double dist = Double.parseDouble(distText);
        double aThree = Double.parseDouble(aThreeText);

        mOne = mOne * aOne;
        mTwo = mTwo * aTwo;
        dist = dist * aThree;
        dist = Math.pow(dist, TWO);

        double total = (G * mOne * mTwo) / dist;

        TextView a = (TextView)findViewById(R.id.finalForceLabel);
        a.setText((int) total);
    }
}

5 个答案:

答案 0 :(得分:1)

这很容易。使用此 a.setText(String.valueOf(total));

package com.hoodeddeath.physicscalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class GravityActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gravity);
    }

    public void calc_Click(View v){
        double TEN = 10;
        double NEGELEVEN = -11;
        double G = 6.67*(Math.pow(TEN,NEGELEVEN));
        double TWO = 2;
        String mOneText = ((EditText)findViewById(R.id.massOne)).getText().toString();
        String aOneText = ((EditText)findViewById(R.id.ampOne)).getText().toString();
        String mTwoText = ((EditText)findViewById(R.id.massTwo)).getText().toString();
        String aTwoText = ((EditText)findViewById(R.id.ampTwo)).getText().toString();
        String distText = ((EditText)findViewById(R.id.distance)).getText().toString();
        String aThreeText = ((EditText)findViewById(R.id.ampThree)).getText().toString();

        double mOne = Double.parseDouble(mOneText);
        double aOne = Double.parseDouble(aOneText);
        double mTwo = Double.parseDouble(mTwoText);
        double aTwo = Double.parseDouble(aTwoText);
        double dist = Double.parseDouble(distText);
        double aThree = Double.parseDouble(aThreeText);

        mOne = mOne * aOne;
        mTwo = mTwo * aTwo;
        dist = dist * aThree;
        dist = Math.pow(dist, TWO);

        double total = (G * mOne * mTwo) / dist;

        TextView a = (TextView)findViewById(R.id.finalForceLabel);
        a.setText(String.valueOf(total));
    }
}

答案 1 :(得分:0)

以下是:

 double total = (G * mOne * mTwo) / dist;

 TextView a = (TextView)findViewById(R.id.finalForceLabel);
 a.setText(String.valueOf(total));

使用String.valueOf(double)功能

答案 2 :(得分:0)

您可以在TextView中设置String。

只需将double转换为String。

a.setText(String.valueOf(total));

有关setText参数的详细信息。请检查此link

答案 3 :(得分:0)

首先,您可能应该检查Array ( [Client] => Array ( [name] => my other Client [Item] => Array ( [0] => 2 [1] => 4 ) ) ) mOne, aOne, mTwo, aTwo, dist是否为空( - 对于原始类型无用),

然后

aThree

答案 4 :(得分:0)

试试这个会起作用

使用Double.toString

a.setText(Double.toString(total));

或者您可以使用NumberFormat:

NumberFormat nm = NumberFormat.getNumberInstance();

a.setText(nm.format(total));