Android studio 带复选框的android中Snackbar小部件的使用
我想使用snackbar在标记某个复选框时显示消息。我看到了各种来源并找到了此消息。对吗Android studio 带复选框的android中Snackbar小部件的使用,android-studio,if-statement,importerror,android-checkbox,android-snackbar,Android Studio,If Statement,Importerror,Android Checkbox,Android Snackbar,我想使用snackbar在标记某个复选框时显示消息。我看到了各种来源并找到了此消息。对吗 CheckBox WhippedCreamCheckBox = (CheckBox) findViewById(R.id.whip_check); final boolean hasWhippedCream = WhippedCreamCheckBox.isChecked(); 这是复选框,我为启用snackbar所做的条件如下:- if(hasWhippedCream){
CheckBox WhippedCreamCheckBox = (CheckBox) findViewById(R.id.whip_check);
final boolean hasWhippedCream = WhippedCreamCheckBox.isChecked();
这是复选框,我为启用snackbar所做的条件如下:-
if(hasWhippedCream){
WhippedCreamCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar whipsnack=Snackbar.make(v,"$1 charged for Whip Cream",Snackbar.LENGTH_SHORT);
whipsnack.show();
}
});
extra=1;
}
(忽略extra)如果正确,则在导入类时显示错误。它表示该类不存在(它自己添加了该类)。导入:-
请帮帮我。
另外,如果您可以,请告诉我们如何添加撤消选项并激活它以取消标记复选框。
这是原始文件:
package com.example.javaup;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
String message;
int extra = 0;
EditText nameEnter =(EditText) findViewById(R.id.name_enter);
String Namedisplay =nameEnter.getText().toString();
CheckBox WhippedCreamCheckBox = (CheckBox) findViewById(R.id.whip_check);
boolean hasWhippedCream = WhippedCreamCheckBox.isChecked();
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_check);
boolean hasChocolate =chocolateCheckBox.isChecked();
if(hasWhippedCream && hasChocolate)
extra=3;
else if(hasChocolate)
extra=2;
else
extra=1;
int price=calculatePrice(extra);
message=createordersummary(price,hasWhippedCream,hasChocolate,Namedisplay);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Coffee order for " + Namedisplay);
intent.putExtra(Intent.EXTRA_TEXT, message);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
displayMessage(message);
}
private String createordersummary(int price,boolean whippedcream ,boolean chocolate,String name)
{
String summary;
summary=getString(R.string.name) + ":" + name + "\n" + getString(R.string.quantity) + ":" + quantity + "\n" + getString(R.string.addwhip) + "?" + whippedcream + "\n" + getString(R.string.addchoc) + "?" + chocolate + "\n" +
getString(R.string.price) + ":$" + price + ".0" + "\n" + getString(R.string.thank);
return summary;
}
public void increment(View view) {
quantity+=1;
display(quantity);
}
public void decrement(View view) {
if(quantity==1){
Toast.makeText(this,"You cannot have less than 1 cup of coffee",Toast.LENGTH_SHORT).show();
return;}
quantity=quantity-1;
display(quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(number));//or we can use ("" + number);
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.price_text_view);
orderSummaryTextView.setText(message);
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice(int extra) {
int price = quantity * (5 + extra);
return price;
}
}
此外,我应在何处插入:
在OnCreate或任何其他地方,因为此复选框变量正在Submitor功能中使用(单击最终按钮激活)
package com.example.javaup;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {
int quantity=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
String message;
int extra = 0;
EditText nameEnter =(EditText) findViewById(R.id.name_enter);
String Namedisplay =nameEnter.getText().toString();
CheckBox WhippedCreamCheckBox = (CheckBox) findViewById(R.id.whip_check);
boolean hasWhippedCream = WhippedCreamCheckBox.isChecked();
CheckBox chocolateCheckBox = (CheckBox) findViewById(R.id.chocolate_check);
boolean hasChocolate =chocolateCheckBox.isChecked();
if(hasWhippedCream && hasChocolate)
extra=3;
else if(hasChocolate)
extra=2;
else
extra=1;
int price=calculatePrice(extra);
message=createordersummary(price,hasWhippedCream,hasChocolate,Namedisplay);
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Coffee order for " + Namedisplay);
intent.putExtra(Intent.EXTRA_TEXT, message);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
displayMessage(message);
}
private String createordersummary(int price,boolean whippedcream ,boolean chocolate,String name)
{
String summary;
summary=getString(R.string.name) + ":" + name + "\n" + getString(R.string.quantity) + ":" + quantity + "\n" + getString(R.string.addwhip) + "?" + whippedcream + "\n" + getString(R.string.addchoc) + "?" + chocolate + "\n" +
getString(R.string.price) + ":$" + price + ".0" + "\n" + getString(R.string.thank);
return summary;
}
public void increment(View view) {
quantity+=1;
display(quantity);
}
public void decrement(View view) {
if(quantity==1){
Toast.makeText(this,"You cannot have less than 1 cup of coffee",Toast.LENGTH_SHORT).show();
return;}
quantity=quantity-1;
display(quantity);
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(String.valueOf(number));//or we can use ("" + number);
}
/**
* This method displays the given text on the screen.
*/
private void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.price_text_view);
orderSummaryTextView.setText(message);
}
/**
* Calculates the price of the order.
*
* @return total price
*/
private int calculatePrice(int extra) {
int price = quantity * (5 + extra);
return price;
}
}