2014-09-30 33 views
0

我現在正面臨此問題。我想生成食物收據及其價格和總價格(所有選中複選框的總和 - 所選菜單)如何在用戶選中複選框時指定整數值

這是複選框頁面的代碼,用戶需要在其中檢查項目喜歡購買。

    <RelativeLayout > 

        <ScrollView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" > 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:orientation="vertical" > 

          <CheckBox 
          android:id="@+id/pakejC1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Beg" /> 

          <CheckBox 
          android:id="@+id/pakejC2" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Shoes" /> 




         <ImageButton 
           android:id="@+id/gobutton" 
           android:layout_width="50dp" 
           android:layout_height="50dp" 
           android:layout_alignTop="@+id/homebtn" 
           android:layout_toRightOf="@+id/homebtn" 
           android:background="@drawable/gobutton" 
           android:onClick="goReceiptC" /> 

這是流程

public class item extends Activity 
     { 

    CheckBox beg1, shoes1; 


    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.item); 

    beg1  = (CheckBox) findViewById(R.id.pakejC1); 
    shoes1  = (CheckBox) findViewById(R.id.pakejC2); 
    } 



public void goReceiptC(View v) 
     { 
      Intent intent = new Intent(v.getContext(), doReceiptC.class); 

      intent.putExtra("beg1", beg1.isChecked()); 
      intent.putExtra("shoes1", shoes1.isChecked()); 

     startActivityForResult(intent,0); 
     } 
    } 

這個代碼是用以顯示收到

public class doReceipt extends Activity 

{ 
    boolean beg1, shoes1; 

    TextView tvOutput1,tvOutput2; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.receipt); 



     tvOutput1 = (TextView) findViewById(R.id.textView1); 
     tvOutput2 = (TextView) findViewById(R.id.textView2); 

     Bundle data = this.getIntent().getExtras(); 

     beg1=data.getBoolean("beg1"); 
     shoes1=data.getBoolean("shoes1"); 

     if(beg2==true) 
     { 

      tvOutput1.setText("Nasi Putih RM 1.00"); 
      tvOutput1.setVisibility(View.VISIBLE); 

    // this is where i would like to display the price 
    //eg rm 1.00 


     } 
     else 
     { 
      tvOutput1.setVisibility(View.GONE); 
     } 

     if (shoes1==true) 
     { 
      tvOutput2.setText("shoes RM 1.50"); 
      tvOutput2.setVisibility(View.VISIBLE); 

     // this is where i would like to display the price 
    //eg rm 1.50 


     } 
     else 
     { 
      tvOutput2.setVisibility(View.GONE); 
     } 

**我想要的總價都檢查項目是顯示的過程在收據上合計

這是顯示收據的頁面

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textA" 
    android:layout_below="@+id/textA" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView1" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView1" 
    android:text="total price" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

幫我PLZ ....

+0

從哪裏獲得價值? 1.50和1? – 2014-09-30 08:07:14

+0

我自己分配價值 – 2014-10-01 00:53:00

回答

0

你是不是從哪裏是這些值來非常清楚。

但是你可以做的是:

//global variables 
float bagPrice = 1.0f, shoesPrice = 1.5f, total = 0f; 

//your logic of getting values 
if(beg2==true) 
{ 
    //do what you want in textviews 
    total += bagPrice; 
} 
else 
{ 
    //your code 
} 

if (shoes1==true) 
{ 
    //do what you want in textviews 
    total += shoesPrice; 
} 
else 
{ 
    //your code 
} 

Toast.makeText(getApplicationContext(), String.valueOf(total), Toast.LENGTH_SHORT).show(); //show total in TextView using String.valueOf(total) 

希望這有助於。

+0

謝謝..幫助 – 2014-10-01 00:53:54

0

您也可以擴展CheckBox並添加一個價格浮動屬性並在構造函數中設置它的值,然後創建一個新的方法getPrice返回0或this.price,具體取決於它是否被選中。

僞代碼

public class PriceCheckBox() extends CheckBox { 

private float price; 

public PriceCheckBox(float price) { 
    super(): 
    this.price = price; 
} 

public float getPrice() { 
    if(isChecked()){ 
     return this.price; 
    } else { 
     return 0; 
    } 
} 

//You could also add a setter here if you wish to change price later. 

然後只需使用PriceCheckBox代替CheckBox和調用用getPrice()代替器isChecked()。

相關問題