Before I asked about the total amount of SIZE and quantity, I also updated my code variable. But I am sorry that I have encountered new materials and new questions now.....
int pearl1 = 10, Cocount1 = 10, Perilla1 = 10,
pudding1 = 10, redbeans1 = 10, aloe1 = 10, jelly1 = 10, jade1 = 10;
less.setOnClickListener( totalprice );
plus.setOnClickListener( totalprice );
Perilla.setOnClickListener( totalprice );
jelly.setOnClickListener( totalprice );
pearl.setOnClickListener( totalprice );
aloe.setOnClickListener( totalprice );
Coconut.setOnClickListener( totalprice );
pudding.setOnClickListener( totalprice );
redbeans.setOnClickListener( totalprice );
jade.setOnClickListener( totalprice );
}
public OnClickListener totalprice = new
OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.less: {
if (count > 0) {
count--;
textView.setText( "" + count );
if (medium.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
buttled.setChecked( false );
} else if (buttled.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count * 0 ) );
count = Integer.parseInt( total_price.getText().toString() );
Toast toast = Toast.makeText( Main20Activity.this, " 無瓶裝! 請重新選擇!", LENGTH_SHORT );
toast.show();
} else {
total_price.setText( Integer.toString( Integer.parseInt( "80" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
buttled.setChecked( false );
}
}
break;
}
case R.id.plus: {
if (count < max) {
count++;
textView.setText( "" + count );
if (large.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "80" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
buttled.setChecked( false );
} else if (buttled.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count * 0 ) );
count = Integer.parseInt( total_price.getText().toString() );
Toast toast = Toast.makeText( Main20Activity.this, " 無瓶裝! 請重新選擇!", LENGTH_SHORT );
toast.show();
} else {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
buttled.setChecked( false );
}
}
break;
}
}
switch (v.getId()) {
case R.id.Perilla: {
counter = counter + Perilla1;
total_price.setText( counter );
}
case R.id.jelly: {
counter = counter + jelly1;
total_price.setText( counter );
}
case R.id.redbeans: {
counter = counter + redbeans1;
total_price.setText( counter );
}
case R.id.jade: {
counter = counter + jade1;
total_price.setText( counter );
}
case R.id.pearl: {
counter = counter + pearl1;
total_price.setText( counter );
}
case R.id.Coconut: {
counter = counter + Cocount1;
total_price.setText( counter );
}
case R.id.pudding: {
counter = counter + pudding1;
total_price.setText( counter );
}
case R.id.aloe: {
counter = counter + aloe1;
total_price.setText( counter );
}
}
}
};
My understanding is: I replaced total_price with "counter". After selecting Perilla's ingredients, total_price should be increased by 10 in the output, isn't it?
I use the case and declare the same setOnClickListener But after I executed it unexpectedly quit
You are only instantiating one variable with the integer 10:
int pearl1, Cocount1, Perilla1,
pudding1, redbeans1, aloe1,
jelly1, jade1 = 10;
Change to:
int pearl1 = 10, Cocount1 = 10, perilla1 = 10, pudding1 = 10, redbeans1 = 10, aloe1 = 10, jelly1 = 10, jade1 = 10;
There was a few issues with your implementation. The first is that you are placing a OnClickListener
inside another OnClickListener
. The second is your break
statements was incorrect. Please see my implementation below:
int pearl1 = 10, Cocount1 = 10, perilla1 = 10, pudding1 = 10, redbeans1 = 10, aloe1 = 10, jelly1 = 10, jade1 = 10;
int max = 99;
private Button.OnClickListener money = new
OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.medium: {
total_price.setText("45");
counter = Integer.parseInt( total_price.getText().toString() );
break;
}
case R.id.large: {
total_price.setText("80");
counter = Integer.parseInt( total_price.getText().toString() );
break;
}
case R.id.bottled: {
total_price.setText( "" );
break;
}
}
less = (ImageButton) findViewById( R.id.less );
plus = (ImageButton) findViewById( R.id.plus );
less.setOnClickListener( AddandLess );
plus.setOnClickListener( AddandLess );
}
});
private Button.OnClickListener AddandLess = new
OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.less: {
if (count > 0) {
count--;
textView.setText( "" + count );
}
break;
}
case R.id.plus: {
if (count < max) {
count++;
textView.setText( "" + count );
}
break;
}
}
}
});
Perilla.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c){
if (c) {
counter=counter+Perilla1;
total_price.setText(counter);
}
}
});
jelly.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c)
if (c) {
counter=counter+jelly1;
total_price.setText(counter);
}
});
aloe.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
if (c) {
counter=counter+aloe1;
total_price.setText(counter);
}
}
});
redbeans.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
if (c) {
counter=counter+redbeans1;
total_price.setText(counter);
}
}
});
pearl.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
if (c) {
counter=counter+pearl1;
total_price.setText(counter);
}
}
});
Coconut.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
if (c) {
counter=counter+Cocount1;
total_price.setText(counter);
}
}
});
jade.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
if (c) {
counter=counter+jade1;
total_price.setText(counter);
}
}
});
pudding.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
if (c) {
counter=counter+pudding1;
total_price.setText(counter);
}
}
});
int TotalPrice = counter * count;
String strTotalPrice = String.valueOf( TotalPrice );
total_price.setText( strTotalPrice );
Edit:
The error mentioned in the comments is because textView
is not initialize by the time it gets called. Add the following in onCreate
:
textView = findViewById(R.id.nameOfTextView);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.