我正在做一个关于计算购物清单中的钱的小程序。 我有一个问题,告诉我选择的确切CheckBox。 该计划如下:
我的布局: https://www.dropbox.com/s/mne4u3va94svx8y/%E6%93%B7%E5%8F%96.JPG?dl=0
我的“activity_main.xml”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose the ingredients that you need" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/check_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Milk 80 $ " />
<CheckBox
android:id="@+id/check_rice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rice 240 $" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/image_milk"
android:layout_width="122dp"
android:layout_height="match_parent"
android:src="@drawable/milk" />
<ImageView
android:id="@+id/image_rice"
android:layout_width="106dp"
android:layout_height="50dp"
android:layout_weight="0.40"
android:src="@drawable/rice" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/check_sausage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sausage 50 $" />
<CheckBox
android:id="@+id/check_chicken"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chicken 350 $" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/image_sausage"
android:layout_width="87dp"
android:layout_height="47dp"
android:src="@drawable/sausage" />
<ImageView
android:id="@+id/image_chicken"
android:layout_width="162dp"
android:layout_height="43dp"
android:layout_weight="0.27"
android:src="@drawable/chicken" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/check_rice_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rice Ball 50 $" />
<CheckBox
android:id="@+id/check_fish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fish 250 $" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/image_rice_ball"
android:layout_width="113dp"
android:layout_height="39dp"
android:src="@drawable/rice_ball" />
<ImageView
android:id="@+id/image_fish"
android:layout_width="192dp"
android:layout_height="match_parent"
android:src="@drawable/fish" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button_buy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buy"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/image_Milk"
android:layout_width="45dp"
android:layout_height="30dp"
android:src="@drawable/milk"
android:visibility="gone" />
<ImageView
android:id="@+id/image_Rice"
android:layout_width="56dp"
android:layout_height="30dp"
android:src="@drawable/rice"
android:visibility="gone" />
<ImageView
android:id="@+id/image_Sausage"
android:layout_width="56dp"
android:layout_height="30dp"
android:src="@drawable/sausage"
android:visibility="gone" />
<ImageView
android:id="@+id/image_Chicken"
android:layout_width="56dp"
android:layout_height="30dp"
android:src="@drawable/chicken"
android:visibility="gone" />
<ImageView
android:id="@+id/image_Rice_Ball"
android:layout_width="56dp"
android:layout_height="30dp"
android:src="@drawable/rice_ball"
android:visibility="gone" />
<ImageView
android:id="@+id/image_Fish"
android:layout_width="56dp"
android:layout_height="30dp"
android:src="@drawable/fish"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ScrollView
android:id="@+id/scroresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/show_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Result"
android:textAppearance="?android:attr/textAppearanceMedium" />
</ScrollView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
我的MainActivity_market.java
package com.example.market_shop_3;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity_market extends Activity
implements OnCheckedChangeListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_market);
//program start here
int[] id={R.id.check_milk, R.id.check_rice,R.id.check_sausage,R.id.check_chicken
,R.id.check_rice_ball,R.id.check_fish};
for(int i:id) //loop for importand recoder listener
{
((CheckBox)findViewById(i)).setOnCheckedChangeListener(this);
}
}
public void buy(View v)
{
String show_buy="";
// String total_prices="";
String test="";
int count=0;
CheckBox check;
int[] id={R.id.check_milk, R.id.check_rice,R.id.check_sausage,R.id.check_chicken
,R.id.check_rice_ball,R.id.check_fish};
for(int i:id)
{
check=(CheckBox)findViewById(i);
if(check.isChecked())
{
show_buy+="\n"+check.getText();
if (check.getText()=="milk 80 $" )
count = count +80;
else if (check.getText()=="Rice 240 $")
count = count +240;
else if (check.getText()=="Sausage 50 $")
count = count +50;
else if (check.getText()=="Chicken 350 $")
count = count +350;
else if (check.getText()=="Rice Ball 50 $")
count = count +50;
else if (check.getText()=="Fish 250 $")
count = count +250;
}
}
int total = count;
if(show_buy.length()>0)
{
show_buy ="Your shopping list: "+show_buy +"\n"+"Total expenses: "+ total;
// total_prices="Total expenses ($): "+ ;
}
else
show_buy="Please pick the ingredients that you need.";
TextView show_result=(TextView)findViewById(R.id.show_result);
show_result.setText(show_buy);
}
//設ArrayList儲存已選項目的集合物件
ArrayList<CompoundButton> selected = new ArrayList<CompoundButton>();
@Override
public void onCheckedChanged(CompoundButton id, boolean isChecked)
{
if (isChecked==true) //若項目被選取
selected.add(id); //加到集合中
else //若項目被取消
selected.remove(id); //自集合中移除
// TODO Auto-generated method stub
int visible;
if(isChecked){ //CHECKED
visible=View.VISIBLE; //SET PICTURE IS VISIBLE
}
else{ //CANCEL CHECKED
visible=View.GONE; //SET PICTURE IS NOT VISIBLE
}
switch (id.getId()){
case R.id.check_milk:
findViewById(R.id.image_Milk).setVisibility(visible);
break;
case R.id.check_rice:
findViewById(R.id.image_Rice).setVisibility(visible);
break;
case R.id.check_sausage:
findViewById(R.id.image_Sausage).setVisibility(visible);
break;
case R.id.check_chicken:
findViewById(R.id.image_Chicken).setVisibility(visible);
break;
case R.id.check_rice_ball:
findViewById(R.id.image_Rice_Ball).setVisibility(visible);
break;
case R.id.check_fish:
findViewById(R.id.image_Fish).setVisibility(visible);
break;
}
}
}
在java部分中,
我不是怎么告诉我选择的CheckBox,我使用了if-elseif条件(check.getText()==“milk 80 $”;否则if(check.getText()==“Rice 240 $” )....) 判断我应该为总费用添加多少钱
我设置一个名为“count”的“int”来累积我的总费用,但似乎没有用。
失败结果: https://www.dropbox.com/s/fwj329jg0sj0ypd/1.JPG?dl=0
“总费用:0”应为“总费用:670”。
答案 0 :(得分:0)
为每个复选框(购物项目)的ID-PRICE创建一个2D数组
int[] id_price={
{R.id.check_milk,80},
{R.id.check_rice,240},
{R.id.check_sausage,50},
{R.id.check_chicken,350},
{R.id.check_rice_ball,50},
{R.id.check_fish}, 250};
单击按钮上的(计算所有选定列表) 或选中或取消选中每个项目(复选框):
CheckBox check = null;
int total = 0;
for(int i=0;i<id_price.length;i++){
check=(CheckBox)findViewById(id_price[i][0]);
if(check.isChecked()){
total += id_price[i][1];//add item price to total
}else{
total -= id_price[i][1];//subtract item price from total
}
}//for all check boxes
//now you have total ready for your next step ...
这种方式不需要IF statement