如何在Android中将double值传递给textview? (我是新来的)

时间:2016-01-09 06:01:40

标签: java android

我正在使用EditText将输入存储为double,然后使用CheckBox修改值并将其存储在变量“output”中。我想在单击计算按钮时在TextView中显示“output”的值。

代码

public class Cooking extends AppCompatActivity {

public void calculate(View view) {
    EditText userInput = (EditText) findViewById(R.id.user_input);
    String numString = userInput.getText().toString();
    double num = new Double(numString).doubleValue();
    CheckBox ozCheckBox = (CheckBox)findViewById(R.id.oz);
    boolean ozInput = ozCheckBox.isChecked();
    CheckBox gCheckBox = (CheckBox)findViewById(R.id.g);
    boolean gInput = gCheckBox.isChecked();
    if(ozInput == true) {
        double output = num*28.3495;
    } else {
        double output = num;
    }
}

3 个答案:

答案 0 :(得分:2)

假设你有一个textView对象

TextView textView = (TextView)findViewById( R.id.myTextView );

String有一个非常方便的方法valueOf() ...

double d = 8008135;
textView.setText( String.valueOf( d ) );

你甚至可以在它前面添加一个空字符串......

textView.setText( "" + d );

两者都会输出

  

8008135.0

答案 1 :(得分:1)

以下是如何操作的指南

double number = -895.25; 
String numberAsString = new Double(number).toString(); 
textview.setText(numberAsString);

答案 2 :(得分:0)

public class Grid extends BaseAdapter {

    private Context mContext;
    private final String[] menu;
    private final int[] Imageid;
    private final String[] gridcolor;

    public Grid(Context context,String[] menu,int[] Imageid,String[] gridcolor)
    {
        mContext=context;

        this.gridcolor=gridcolor;
    }

    @Override
    public int getCount() {
        return gridcolor.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (view == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_layout, null);

            grid.setBackgroundColor(Color.parseColor(gridcolor[i]));

        } else
        {
            grid =  view;
        }

        return grid;
    }
}