Java 如何在Android中使用按钮执行多个操作
我有一个电子商务应用程序,通过点击确认订单按钮,我需要一个通知来显示和下订单,以及发送邮件给客户。我已经做了,但不知道它是否有效。在这件事上有人能帮忙吗?我将分享下面的代码 公共类确认活动扩展了AppCompatActivity{Java 如何在Android中使用按钮执行多个操作,java,android,firebase,button,Java,Android,Firebase,Button,我有一个电子商务应用程序,通过点击确认订单按钮,我需要一个通知来显示和下订单,以及发送邮件给客户。我已经做了,但不知道它是否有效。在这件事上有人能帮忙吗?我将分享下面的代码 公共类确认活动扩展了AppCompatActivity{ private EditText nameEditText, phoneEditText,emailEditText, addressEditText, cityEditText; private Button confirmOrderbtn; private Str
private EditText nameEditText, phoneEditText,emailEditText, addressEditText, cityEditText;
private Button confirmOrderbtn;
private String totalAmount = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirm);
totalAmount = getIntent().getStringExtra("Total Price");
confirmOrderbtn = (Button) findViewById(R.id.confirm_final_order_btn);
nameEditText = (EditText) findViewById(R.id.shipment_name);
phoneEditText = (EditText) findViewById(R.id.shipment_phone);
emailEditText = (EditText) findViewById(R.id.shipment_email);
addressEditText = (EditText) findViewById(R.id.shipment_address);
cityEditText = (EditText) findViewById(R.id.shipment_city);
confirmOrderbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
check();
}
});
}
private void check() {
if (TextUtils.isEmpty(nameEditText.getText().toString())) {
Toast.makeText(this, "Please provide your full name", Toast.LENGTH_LONG).show();
} else if (TextUtils.isEmpty(phoneEditText.getText().toString())) {
Toast.makeText(this, "Please provide your Phone number", Toast.LENGTH_LONG).show();
}
else if (TextUtils.isEmpty(emailEditText.getText().toString())) {
Toast.makeText(this, "Please provide your Email id", Toast.LENGTH_LONG).show();
}
else if (TextUtils.isEmpty(addressEditText.getText().toString()))
{
Toast.makeText(this, "Please provide your Address", Toast.LENGTH_LONG).show();
} else if (TextUtils.isEmpty(cityEditText.getText().toString())) {
Toast.makeText(this, "Please provide your city", Toast.LENGTH_LONG).show();
} else {
ConfirmOrder();
addNotification();
}
}
private void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.bell) //set icon for notification
.setContentTitle("Order Placed") //set title of notification
.setContentText("Thank you")//this is notification message
.setAutoCancel(true) // makes auto cancel of notification
.setPriority(NotificationCompat.PRIORITY_DEFAULT); //set priority of notification
Intent notificationIntent = new Intent(this, NotificationView.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//notification message will get at NotificationView
notificationIntent.putExtra("message", "Thank you");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
private void ConfirmOrder() {
final String saveCurrentTime, saveCurrentDate;
Calendar CallForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("MMM dd,yyyy");
saveCurrentDate = currentDate.format(CallForDate.getTime());
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss a");
saveCurrentTime = currentDate.format(CallForDate.getTime());
final DatabaseReference ordersRef = FirebaseDatabase.getInstance().getReference().child("Orders")
.child(Prevalent.currentOnlineUser.getPhone());
HashMap<String, Object> ordersMap = new HashMap<>();
ordersMap.put("totalAmount", totalAmount);
ordersMap.put("name", nameEditText.getText().toString());
ordersMap.put("phone", phoneEditText.getText().toString());
ordersMap.put("email", emailEditText.getText().toString());
ordersMap.put("address", addressEditText.getText().toString());
ordersMap.put("city", cityEditText.getText().toString());
ordersMap.put("date", saveCurrentDate);
ordersMap.put("time", saveCurrentTime);
ordersMap.put("state", "not shipped");
ordersRef.updateChildren(ordersMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
FirebaseDatabase.getInstance().getReference().
child("Cart List")
.child("User View")
.child(Prevalent.currentOnlineUser.getPhone())
.removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(confirmActivity.this, "Your final order has been succesfully placed", Toast.LENGTH_LONG).show();
Intent intent = new Intent(confirmActivity.this, HomeActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
});
}
}
});
}
私有编辑文本名称编辑文本、电话编辑文本、电子邮件编辑文本、地址编辑文本、城市编辑文本;
私人按钮确认器;
私有字符串totalAmount=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_确认);
totalAmount=getIntent().getStringExtra(“总价”);
confirmOrderbtn=(按钮)findViewById(R.id.confirm\u final\u order\u btn);
nameEditText=(EditText)findViewById(R.id.Shipping\U名称);
phoneEditText=(EditText)findViewById(R.id.shipping\u电话);
emailEditText=(EditText)findViewById(R.id.Shipping\u电子邮件);
addressEditText=(EditText)findViewById(R.id.Shipping_地址);
cityEditText=(EditText)findViewById(R.id.Shipping\u city);
confirmOrderbtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
检查();
}
});
}
私人作废检查(){
if(TextUtils.isEmpty(nameEditText.getText().toString()){
Toast.makeText(这是“请提供您的全名”,Toast.LENGTH_LONG.show();
}else if(TextUtils.isEmpty(phoneEditText.getText().toString()){
Toast.makeText(这是“请提供您的电话号码”,Toast.LENGTH_LONG.show();
}
else if(TextUtils.isEmpty(emailEditText.getText().toString()){
Toast.makeText(这是“请提供您的电子邮件id”,Toast.LENGTH_LONG.show();
}
else if(TextUtils.isEmpty(addressEditText.getText().toString())
{
Toast.makeText(这是“请提供您的地址”,Toast.LENGTH_LONG.show();
}else if(TextUtils.isEmpty(cityEditText.getText().toString()){
Toast.makeText(这是“请提供您的城市”,Toast.LENGTH_LONG.show();
}否则{
确认人();
添加通知();
}
}
私有void addNotification(){
NotificationCompat.Builder=
新建NotificationCompat.Builder(此)
.setSmallIcon(R.drawable.bell)//设置通知图标
.setContentTitle(“下单”)//设置通知的标题
.setContentText(“谢谢”)//这是通知消息
.setAutoCancel(true)//自动取消通知
.setPriority(NotificationCompat.PRIORITY_默认);//设置通知的优先级
Intent notificationIntent=新的Intent(这个,NotificationView.class);
notificationIntent.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
//通知消息将在NotificationView中获取
notificationIntent.putExtra(“消息”,“谢谢”);
PendingEvent PendingEvent=PendingEvent.getActivity(this,0,notificationIntent,
PendingEvent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(挂起内容);
//添加为通知
NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
manager.notify(0,builder.build());
}
私人无效确认人(){
最终字符串saveCurrentTime,saveCurrentDate;
Calendar CallForDate=Calendar.getInstance();
SimpleDataFormat currentDate=新的SimpleDataFormat(“MMM dd,yyyy”);
saveCurrentDate=currentDate.format(CallForDate.getTime());
SimpleDataFormat currentTime=新的SimpleDataFormat(“HH:mm:ss a”);
saveCurrentTime=currentDate.format(CallForDate.getTime());
final DatabaseReference ordersRef=FirebaseDatabase.getInstance().getReference().child(“订单”)
.child(流行的.currentOnlineUser.getPhone());
HashMap ordersMap=newhashmap();
订单应付卖出价(“总金额”,总金额);
ordersMap.put(“name”,nameEditText.getText().toString());
ordersMap.put(“phone”,phoneEditText.getText().toString());
ordersMap.put(“email”,emailEditText.getText().toString());
ordersMap.put(“地址”,addressEditText.getText().toString());
ordersMap.put(“city”,cityEditText.getText().toString());
订单映射放置(“日期”,保存当前日期);
ordersMap.put(“时间”,saveCurrentTime);
订单映射放置(“状态”、“未装运”);
ordersRef.updateChildren(ordersMap.addOnCompleteListener(新的OnCompleteListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
FirebaseDatabase.getInstance().getReference()。
子项(“购物车列表”)
.child(“用户视图”)
.child(通用.currentOnlineUser.getPhone())
.removeValue()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
Toast.makeText(confirActivity.this,“您的最终订单已成功下达”,Toast.LENGTH\u LONG.show();
意向意向=新意向(confirActivity.this、HomeActivity2.class);
intent.addFlags(intent.FLAG_活动_清除_任务| intent.FLAG_活动_清除_任务);
星触觉(意向);
完成();
}
}
});
}
}
});
}
}移动添加通知()如果(task.issusccessful()){
的onComplete
完成firebase的中,那么您只能确保
private boolean isOrderConfirmed = false; <-- This one.
@Override
protected void onCreate(Bundle savedInstanceState) {
...
@Override
public void onComplete(@NonNull Task<Void> task) {
isOrderConfirmed = false; //Reset <-- This one
if (task.isSuccessful()) {
...
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
isOrderConfirmed = true; //<-- And this one.
Toast.makeText(confirmActivity.this, "Your final order has been succesfully placed", Toast.LENGTH_LONG).show();
...
private void check() {
...
} else {
ConfirmOrder();
if(isOrderConfirmed) {
addNotification(); <-- This one.
// doSomething();
}
}