2014-10-12 59 views
0

我一直無法弄清楚,但我試圖做的是將我的應用程序中的幾個雙變量轉換爲字符串。我不完全確定如何在我目前的代碼中做到這一點。我一直得到「不能在原始類型double中調用isChecked()」。我無法找到任何教程解釋如何做到這一點,所以原諒我。這裏是我的代碼,任何提示將不勝感激:將double轉換爲單選按鈕的字符串

package net.androidbootcamp.zipcarrentalapp; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity 
{ 
    private RadioGroup radioGroupId; 
    private RadioButton radioButton; 
    private Button button; 
     final RadioButton Compact = (RadioButton)findViewById(R.id.radCompact); 
     final RadioButton MidSize = (RadioButton)findViewById(R.id.radMidSize); 
     final RadioButton Luxury = (RadioButton)findViewById(R.id.radLuxury); 
     final TextView result = (TextView) findViewById(R.id.txtResult); 
     double radCompact = 59.99; 
     double radMidSize = 65.99; 
     double radLuxury = 89.99; 
     int days = 0; 
     double computeValue; 

     public void operation(int days) 
     { 
      if(days <= 10) 
      { 
       if(radCompact.isChecked()) 
       { 
        computeValue = radCompact * days; 
        txtResult.setText("Cost:" + Double.toString(computeValue)); 

       } 

       else if (radMidSize.isChecked()) 
       { 
        computeValue = radMidSize * days; 
        txtResult.setText("Cost: " + Double.toString(computeValue)); 
       } 
       else if (radLuxury.isChecked()) 
       { 
        computeValue = radLuxury * days; 
        txtResult.setText("Cost:" + Double.toString(computeValue)); 
       } 
      } 
     } 

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


    @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; 
    } 

    @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); 
    } 
} 

回答

0

您的代碼是有點關閉。您有三個按鈕,

final RadioButton Compact = (RadioButton)findViewById(R.id.radCompact); 
final RadioButton MidSize = (RadioButton)findViewById(R.id.radMidSize); 
final RadioButton Luxury = (RadioButton)findViewById(R.id.radLuxury); 

您對它們調用isChecked()。例如,這

if(radCompact.isChecked()) 

應該

if (Compact.isChecked()) 

然後

else if (MidSize.isChecked()) 

最後

else if (Luxury.isChecked()) 

編輯而且,

final TextView result = (TextView) findViewById(R.id.txtResult); 

應該

final TextView txtResult = (TextView) findViewById(R.id.txtResult); 
+0

太感謝你了,它固定它。我感謝所有的幫助:) – 2014-10-12 01:32:03

+0

@AverageJoe編輯。 – 2014-10-12 01:34:24

0

你可以嘗試給變量類型強制轉換爲Double對象。

喜歡的東西:

((Double) computeValue).toString 
相關問題