【发布时间】:2019-10-06 00:53:48
【问题描述】:
我正在使用电子邮件意图是我的第二个活动,没有错误,但是当单击按钮时,电子邮件意图没有启动并返回到我的 java 应用程序。请帮帮我!
这是我的secondactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg">
<TextView
android:id="@+id/header"
android:text="MAKAN DAN MINUMAN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="@color/colorWhitebone"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/gado"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/gado"
android:layout_centerHorizontal="true"
android:layout_below="@id/header">
</ImageView>
<TextView
android:id="@+id/tgd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gado Gado"
android:textColor="@color/colorWhitebone"
android:textSize="25dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/gado">
</TextView>
<ImageView
android:id="@+id/ka"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_below="@id/plus"
android:layout_centerHorizontal="true"
android:src="@drawable/ka">
</ImageView>
<TextView
android:id="@+id/tka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kopi Aceh"
android:textColor="@color/colorWhitebone"
android:textSize="25dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/ka">
</TextView>
<Button
android:id="@+id/minus2"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:onClick="decrement2"
android:layout_centerHorizontal="true"
android:layout_below="@id/tka"/>
<TextView
android:id="@+id/quantity_text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/initial_quantity_value"
android:textColor="@android:color/white"
android:textSize="16sp"
android:layout_centerHorizontal="true"
android:layout_below="@id/minus2"
/>
<Button
android:id="@+id/plus2"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:onClick="increment2"
android:layout_centerHorizontal="true"
android:layout_below="@id/quantity_text_view2"
/>
<Button
android:id="@+id/minus"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:onClick="decrement"
android:layout_centerHorizontal="true"
android:layout_below="@id/tgd"/>
<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="@string/initial_quantity_value"
android:textColor="@android:color/white"
android:textSize="16sp"
android:layout_centerHorizontal="true"
android:layout_below="@id/minus"
/>
<Button
android:id="@+id/plus"
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:onClick="increment"
android:layout_centerHorizontal="true"
android:layout_below="@id/quantity_text_view"
/>
<Button
android:id="@+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_below="@id/gado"
android:onClick="submitOrder"
android:text="@string/order" />
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</ScrollView>
这是我的第二个 java
package com.example.moveintent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.text.NumberFormat;
import android.text.Editable;
import android.content.Intent;
import android.net.Uri;
public class SecondActivity extends AppCompatActivity implements View.OnClickListener
{
private Button btnBack;
int quantity = 0;
int quantity2 =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btnBack = findViewById(R.id.btn_back);
btnBack.setOnClickListener(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void increment(View view){
if (quantity == 100){
//show an error message as a toast
Toast.makeText(this,"You cannot have more than 100 foods/drinks", Toast.LENGTH_SHORT ).show();
//exit this method early because nothing to do
return;
}
quantity = quantity + 1;
displayQuantity(quantity);
}
public void decrement(View view){
if (quantity == 1){
//show an error message as a toast
Toast.makeText(this,"You cannot have less than 1 foor/drink", Toast.LENGTH_SHORT ).show();
//exit this method early because nothing to do
return;
}
quantity = quantity - 1;
displayQuantity(quantity);
}
public void increment2(View view){
if (quantity2 == 100){
//show an error message as a toast
Toast.makeText(this,"You cannot have more than 100 foods/drinks", Toast.LENGTH_SHORT ).show();
//exit this method early because nothing to do
return;
}
quantity2 = quantity2 + 1;
displayQuantity2(quantity2);
}
public void decrement2(View view){
if (quantity2 == 1){
//show an error message as a toast
Toast.makeText(this,"You cannot have less than 1 foor/drink", Toast.LENGTH_SHORT ).show();
//exit this method early because nothing to do
return;
}
quantity2 = quantity2 - 1;
displayQuantity2(quantity2);
}
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
// Get user's name
EditText nameField = (EditText) findViewById(R.id.name_field);
Editable nameEditable = nameField.getText();
String name = nameEditable.toString();
//get user's number
EditText hpField = (EditText) findViewById(R.id.hp_field);
Editable nomorEditable = hpField.getText();
String nomor = nomorEditable.toString();
// Figure out if the user wants pempek
CheckBox pempekCheckBox = (CheckBox) findViewById(R.id.pempek_checkbox);
boolean hasPempek = pempekCheckBox.isChecked();
// Figure out if the user wants kue gandus
CheckBox gandusCheckBox = (CheckBox) findViewById(R.id.gandus_checkbox);
boolean hasGandus = gandusCheckBox.isChecked();
// Figure out if the user wants bongkol
CheckBox bongkolCheckBox = (CheckBox) findViewById(R.id.bongkol_checkbox);
boolean hasBongkol = bongkolCheckBox.isChecked();
// Figure out if the user wants risol
CheckBox risolCheckBox = (CheckBox) findViewById(R.id.risol_checkbox);
boolean hasRisol = risolCheckBox.isChecked();
// Calculate the price
int price = calculatePrice(hasPempek, hasGandus , hasBongkol , hasRisol);
// Display the order summary on the screen
String priceMessage = createOrderSummary(name, nomor, price, hasPempek, hasGandus , hasBongkol , hasRisol);
// Use an intent to launch an email app.
// Send the order summary in the email body.
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, "Just java order for " + name);
intent.putExtra(Intent.EXTRA_TEXT,priceMessage );
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
/**
* Calculates the price of the order.
*
* @param addPempek is whether or not we should include pempek in the price
* @param addGandus is whether or not we should include gandus in the price
* @param addBongkol is whether or not we should include bongkol in the price
* @param addRisol is whether or not we should include risol in the price
* @return total price
*/
private int calculatePrice(boolean addPempek, boolean addGandus , boolean addBongkol , boolean addRisol) {
// First calculate the price of one appetizer
int basePrice = 5;
// If the user wants pempek, add 10000
if (addPempek) {
basePrice = basePrice + 10000;
}
// If the user wants kue gandus, add 10000
if (addGandus) {
basePrice = basePrice + 10000;
}
// If the user wants bongkol, add 10000
if (addBongkol) {
basePrice = basePrice + 10000;
}
// If the user wants risol, add 10000
if (addRisol) {
basePrice = basePrice + 10000;
}
// Calculate the total order price by multiplying by the quantity
return quantity + quantity2 * basePrice;
}
/**
* Create summary of the order.
*
* @param name on the order
* @param price of the order
* @param addPempek is whether or not to add pempek
* @param addGandus is whether or not to add kue gandus
* @param addBongkol is whether or not to add bongkol
* @param addRisol is whether or not to add risol
* @return text summary
*/
private String createOrderSummary(String name,String nomor, int price,boolean addPempek, boolean addGandus , boolean addBongkol , boolean addRisol) {
String priceMessage = getString(R.string.order_summary_name, name);
priceMessage = getString(R.string.order_summary_nomor, nomor);
priceMessage += "\n" + getString(R.string.order_summary_pempek, addPempek);
priceMessage += "\n" + getString(R.string.order_summary_gandus, addGandus);
priceMessage += "\n" + getString(R.string.order_summary_bongkol, addBongkol);
priceMessage += "\n" + getString(R.string.order_summary_risol, addRisol);
priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity);
priceMessage += "\n" + getString(R.string.order_summary_quantity, quantity);
priceMessage += "\n" + getString(R.string.order_summary_price,
NumberFormat.getCurrencyInstance().format(price));
priceMessage += "\n" + getString(R.string.thank_you);
return priceMessage;
}
private void displayQuantity(int numberOfFoods) {
TextView quantityTextView = (TextView) findViewById(
R.id.quantity_text_view);
quantityTextView.setText("" + numberOfFoods);
}
private void displayQuantity2(int numberOfFoods) {
TextView quantityTextView2 = (TextView) findViewById(
R.id.quantity_text_view2);
quantityTextView2.setText("" + numberOfFoods);
}
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_back:
finish();
break;
}
}
}
【问题讨论】:
标签: java android android-intent gmail