我的应用程序有两个按钮,在第一个按钮工作时执行各种任务(id:“CalcBtn”)但是,我想打开浏览器的第二个按钮(id:web)不起作用。当我打开模拟器时,第二个按钮的点击器不会启动网站甚至检测到点击本身。
public class MainActivity extends AppCompatActivity {
EditText editText;
EditText editText2;
EditText editText3;
Button CalcBtn;
Button web;
TextView Output;
double a, b, c, d, x1, x2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
CalcBtn = (Button) findViewById(R.id.CalcBtn);
web = (Button) findViewById(R.id.web);
Output = (TextView) findViewById(R.id.Output);
CalcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!editText.getText().toString().equals("") && !editText2.getText().toString().equals("")
&& !editText3.getText().toString().equals("")) {
a = Double.parseDouble(editText.getText().toString());
b = Double.parseDouble(editText2.getText().toString());
c = Double.parseDouble(editText3.getText().toString());
d = Math.pow(b, 2) - 4*a*c;
if (d == 0) {
x1 = -b / (2*a);
Output.setText("d = " + d + "\nx = " + x1);
} else if (d < 0) {
Output.setText("No Real Roots");
} else if (d > 0) {
x1 = (-b + Math.sqrt(d))/(2*a);
x2 = (-b - Math.sqrt(d))/(2*a);
Output.setText("d = " + d + "\nx = " + x1 + "\nx2 = " + x2);
web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent web = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.purplemath.com/modules/quadform.htm"));
startActivity(web);
}
});
}
}
}
});
}
}
答案 0 :(得分:0)
将您的web
按钮点击事件置于点击事件CalcBtn
之外。
web
按钮点击监听器位于CalcBtn
点击事件内,甚至无法检测到。
网页按钮点击事件
web.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent web = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.purplemath.com/modules/quadform.htm"));
startActivity(web);
}
});
CalcBtn按钮点击事件
CalcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!editText.getText().toString().equals("") && !editText2.getText().toString().equals("")
&& !editText3.getText().toString().equals("")) {
a = Double.parseDouble(editText.getText().toString());
b = Double.parseDouble(editText2.getText().toString());
c = Double.parseDouble(editText3.getText().toString());
d = Math.pow(b, 2) - 4*a*c;
if (d == 0) {
x1 = -b / (2*a);
Output.setText("d = " + d + "\nx = " + x1);
} else if (d < 0) {
Output.setText("No Real Roots");
} else if (d > 0) {
x1 = (-b + Math.sqrt(d))/(2*a);
x2 = (-b - Math.sqrt(d))/(2*a);
Output.setText("d = " + d + "\nx = " + x1 + "\nx2 = " + x2);
}
}
}
});