2014-09-30 2 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 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); 
     } 

이 ** 내가 모두 선택 항목에 대한 총 금액이 표시되고 싶어 디스플레이 영수증에 대한 처리 과정

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

이에 대한 코드입니다 총 영수증에

영수증을 표시하는 페이지

<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? –

+0

직접 값을 할당합니다. –

답변

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

감사합니다. 도움이됩니다. –

0

또한 CheckBox를 확장하고 price float 속성을 추가하고 생성자에서 해당 값을 설정 한 다음 검사 여부에 따라 0 또는 this.price를 반환하는 새 메소드 getPrice를 만들 수 있습니다.

의사 코드

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를 사용하고 getPrice 전화() 대신의 IsChecked().

관련 문제