如何在eclipse ADT上将textview转换为double?

时间:2014-09-18 13:31:47

标签: java android eclipse string double

我正在尝试开发计算器,并发现当使用dot作为十进制时,它强制关闭。我问过我的电脑老师,但他说只要加倍...我知道怎么样,因为我还在这里。因为我需要加倍值,目前只支持整数。 这是我的代码

    package id.duo.ka.mycalc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView disp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        disp=(TextView)findViewById(R.id.textView1);
        disp.setText("0");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    static boolean isempty=true;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void num_Clicked(View sender)
    {
        Button bt=(Button)sender;
        if(disp.getText().length()>7)return;
        if(isempty)
            {
                if(bt.getText().toString().equals("0"))return;
                disp.setText(bt.getText());
                isempty=false;
            }
        else 
            {
                disp.append(bt.getText());
            }
    }
    static int accumulator=0;
    static short operationToDo=0;
    public void op_Clicked(View sender)
    {
        Button bt=(Button)sender;
        switch (operationToDo)
        {
        case 0:
            accumulator+=Integer.parseInt(disp.getText().toString());
        break;
        case 1:
            accumulator-=Integer.parseInt(disp.getText().toString());
        break;
        case 2:
            accumulator/=Integer.parseInt(disp.getText().toString());
        break;
        case 3:
            accumulator*=Integer.parseInt(disp.getText().toString());
        break;
        case 4:
            accumulator=Integer.parseInt(disp.getText().toString());
        break;
        }
        disp.setText(Integer.toString(accumulator));
        if(bt.getText().toString().equals("+")) operationToDo=0;
        if(bt.getText().toString().equals("-")) operationToDo=1;
        if(bt.getText().toString().equals("/")) operationToDo=2;
        if(bt.getText().toString().equals("*")) operationToDo=3;
        if(bt.getText().toString().equals("=")) operationToDo=4;
        isempty=true;
    }

    public void clearDisplay (View view) {
        disp=(TextView)findViewById(R.id.textView1);
        disp.setText("0");
        accumulator = 0 ;
    }



 public void backspace (View view) {
     String str = disp.getText().toString();
     if (str.length() >1 ) {         
         str = str.substring(0, str.length() -1);
         disp.setText(str);
     }
     else if (str.length() <=1 ) {
         disp.setText("0");
     }
 }


}

我还是一个新手,我跟着一个youtube vid跟着它直到我到目前为止...当然有一点点的模式..我之前搜索过这个,找到了几个答案,但我仍然不明白怎么做。 任何想法?

3 个答案:

答案 0 :(得分:0)

如果你想使用双打而不是整数,你应该:

  • 使用Double.parseDouble()代替Integer.parseInt()
  • accumulator成为double,而不是int
  • 使用Double.toString()(甚至更好,String.format())代替Integer.toString()

答案 1 :(得分:0)

只需使用:

double d = Double.parseDouble(yourTextView.getText().toString());

答案 2 :(得分:0)

你可以将一个int转换成双重

double myDouble = (double)5

将你的int乘以1.0

double myDouble = 5*1.0

如果要将字符串转换为double,请使用

double myDouble = Double.parseDouble("5")