我有一个有2个按钮的应用程序:一个进行计算,另一个用浏览器启动网站。当用户单击button1时,应显示答案。当用户点击button2时,浏览器会将他们引导到网站。问题是:单击button1会导致弹出“使用完整操作”对话框,提示用户选择4种内容之一(应用程序,进程等)。这种情况以前没有发生,而且很奇怪。我唯一能想到的可能是onclicklistener的问题,而button1没有新的意图,所以我不知道这个对话框是怎么出现的。
这是清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.costaload"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的代码:
package com.example.costaload;
import com.example.costloads.R;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
EditText mile, diesel;
Button button1, button2;
TextView tv, tv2, tv3;
private double x, y, z, costper, gallon, litres, ophours, stopdrive;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
NumberFormat format = NumberFormat.getCurrencyInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mile = (EditText) findViewById(R.id.mile);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(button1onClickListener);
OnClickListener button2onClickListener = null;
button2.setOnClickListener(button2onClickListener);
tv = (TextView) findViewById(R.id.cost);
tv2 = (TextView) findViewById(R.id.cpm);
tv3 = (TextView) findViewById(R.id.gallons);
diesel = (EditText) findViewById(R.id.diesel);
}
private OnClickListener button1onClickListener = new OnClickListener() {
public void onClick(final View v) {
x = Double.parseDouble(mile.getText().toString());
y = Double.parseDouble(diesel.getText().toString());
if (checkBox2.isChecked()) {
x = x * 2;
}
if (checkBox1.isChecked()) {
x = x * 0.62137;
}
ophours = 0;
ophours = (x / 55) + 2;
if (ophours >= 11) {
stopdrive = (ophours / 10) - 1;
if (stopdrive > 1) { // This block isn't closed.
ophours = ophours + (stopdrive * 10);
}
gallon = x / 5.5;
if (checkBox4.isChecked()) {
gallon = gallon + (ophours * 1.1);
}
if (checkBox3.isChecked()) {
litres = gallon * 3.785;
tv3.setText(new DecimalFormat("####.##").format(litres)
+ "L");
}
z = (gallon * y) + (x * 0.655);
costper = z / x;
tv.setText(format.format(z));
tv2.setText(format.format(costper) + "/mile");
tv3.setText(new DecimalFormat("####.##").format(gallon)
+ "gal.");
}
;
};
private OnClickListener button2OnClickListener = new OnClickListener() {
public void onClick(final View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Url.parse("http://www.ratenroll.com"));
startActivity(browserIntent);
}
};
};
};
这是活动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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="# of miles"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="40dp"
android:text="Price of diesel"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/diesel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/mile"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/mile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/checkBox3"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Visit www.ratenroll.com" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/diesel"
android:layout_marginTop="14dp"
android:text="Kilometres (not miles)" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignParentRight="true"
android:text="Round trip" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox1"
android:text="Litres (not gallons)" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox3"
android:layout_alignBottom="@+id/checkBox3"
android:layout_alignParentRight="true"
android:text="Reefer load" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox3"
android:layout_marginTop="50dp"
android:layout_toRightOf="@+id/textView2"
android:text="Compute" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="26dp"
android:text="Total Cost"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:text="Cost per mile"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignParentRight="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/cpm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentRight="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView4"
android:layout_below="@+id/textView4"
android:text="Fuel required"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/gallons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignParentRight="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
感谢您的关注。
答案 0 :(得分:1)
这支额外支撑在这里做什么?
tv3.setText(new DecimalFormat("####.##").format(gallon)+"gal.");
break;} // did you mean to do this?
在第二个案例陈述中将大括号移到break;
之后。
P.S。请养成正确缩进代码和排列大括号的习惯。还可以使用IDE的自动代码格式化功能来保持整洁。它会让你的事情变得更轻松。
P.P.S。对于其他读者。为什么这个甚至编译,因为第二个case语句现在在switch之外?
答案 1 :(得分:1)
将相同的onClickListener添加到两个按钮并使用switch语句区分它们的方法很容易出错。
package com.example.costaload;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
EditText mile, diesel;
Button button1, button2;
TextView tv, tv2, tv3;
private double x, y, z, costper, gallon, litres, ophours, stopdrive;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
NumberFormat format = NumberFormat.getCurrencyInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mile = (EditText) findViewById(R.id.mile);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(button1onClickListener);
button2.setOnClickListener(button2OnClickListener);
tv = (TextView) findViewById(R.id.cost);
tv2 = (TextView) findViewById(R.id.cpm);
tv3 = (TextView) findViewById(R.id.gallons);
diesel = (EditText) findViewById(R.id.diesel);
}
private OnClickListener button1onClickListener = new OnClickListener() {
public void onClick(final View v) {
x = Double.parseDouble(mile.getText().toString());
y = Double.parseDouble(diesel.getText().toString());
if (checkBox2.isChecked()) {
x = x * 2;
}
if (checkBox1.isChecked()) {
x = x * 0.62137;
}
ophours = 0;
ophours = (x / 55) + 2;
if (ophours >= 11) {
stopdrive = (ophours / 10) - 1;
}
if (stopdrive > 1) {
ophours = ophours + (stopdrive * 10);
}
gallon = x / 5.5;
if (checkBox4.isChecked()) {
gallon = gallon + (ophours * 1.1);
}
if (checkBox3.isChecked()) {
litres = gallon * 3.785;
tv3.setText(new DecimalFormat("####.##").format(litres) + "L");
}
z = (gallon * y) + (x * 0.655);
costper = z / x;
tv.setText(format.format(z));
tv2.setText(format.format(costper) + "/mile");
tv3.setText(new DecimalFormat("####.##").format(gallon) + "gal.");
}
};
private OnClickListener button2OnClickListener = new OnClickListener() {
public void onClick(final View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.ratenroll.com"));
startActivity(browserIntent);
}
};
};
我没有更改你的xml文件。
祝你好运。