我正在研究简单的计算器程序。我试着编写一个代码,它会返回我按下的按钮的ID(+, - ,*或/)。
我做了什么,我使用了2个TEXTBOX,其中一个将保留一个数字& " ="按钮会做数学&在TX1上显示结果。 我试着使用SWITCH选项。 出于某种原因,当我按下" ="它没有用。
我将感谢你们的帮助。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
private Button B1,B2,B3,B4,B5,B6,B7,B8,B9,B0;
private Button BMINUS,BSHAVE,BDIVIDE,BBACK,BCLEAR,BPOINT,BDUAL,BPLUS;
private TextView TVDISPLAY,TVDISPLAY2;
double x=0,y=0,z=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
B1 = (Button) findViewById(R.id.bt1);
B1.setOnClickListener(this);
B2 = (Button) findViewById(R.id.bt2);
B2.setOnClickListener(this);
B3 = (Button) findViewById(R.id.bt3);
B3.setOnClickListener(this);
B4 = (Button) findViewById(R.id.bt4);
B4.setOnClickListener(this);
B5 = (Button) findViewById(R.id.bt5);
B5.setOnClickListener(this);
B6 = (Button) findViewById(R.id.bt6);
B6.setOnClickListener(this);
B7 = (Button) findViewById(R.id.bt7);
B7.setOnClickListener(this);
B8 = (Button) findViewById(R.id.bt8);
B8.setOnClickListener(this);
B9 = (Button) findViewById(R.id.bt9);
B9.setOnClickListener(this);
B0 = (Button) findViewById(R.id.bt0);
B0.setOnClickListener(this);
BMINUS = (Button) findViewById(R.id.btsub);
BMINUS.setOnClickListener(this);
BSHAVE = (Button) findViewById(R.id.btequal);
BSHAVE.setOnClickListener(this);
BPLUS= (Button) findViewById(R.id.btadd);
BPLUS.setOnClickListener(this);
BDIVIDE= (Button) findViewById(R.id.btdivide);
BDIVIDE.setOnClickListener(this);
BBACK= (Button) findViewById(R.id.btback);
BBACK.setOnClickListener(this);
BCLEAR = (Button) findViewById(R.id.btclear);
BCLEAR.setOnClickListener(this);
BPOINT = (Button) findViewById(R.id.btpoint);
BPOINT.setOnClickListener(this);
BDUAL = (Button) findViewById(R.id.btdual);
BDUAL.setOnClickListener(this);
TVDISPLAY = (TextView) findViewById(R.id.TV1);
TVDISPLAY.setOnClickListener(this);
TVDISPLAY.setText(" ");
TVDISPLAY.setVisibility(View.VISIBLE); // textview visible
TVDISPLAY2 = (TextView) findViewById(R.id.TV2);
TVDISPLAY2.setOnClickListener(this);
TVDISPLAY2.setText(" ");
TVDISPLAY2.setVisibility(View.INVISIBLE); // textview2 invisible
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void OnClickListener(){
}
@Override
public void onClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.bt0:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "0");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "0");
}
break;
case R.id.bt1:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "1");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "1");
}
break;
case R.id.bt2:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "2");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "2");
}
break;
case R.id.bt3:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "3");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "3");
}
break;
case R.id.bt4:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "4");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "4");
}
break;
case R.id.bt5:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "5");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "5");
}
break;
case R.id.bt6:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "6");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "6");
}
break;
case R.id.bt7:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "7");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "7");
}
break;
case R.id.bt8:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "8");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "8");
}
break;
case R.id.bt9:
if (TVDISPLAY.getVisibility() == View.VISIBLE) {
TVDISPLAY.setText(TVDISPLAY.getText() + "9");
}
else {
TVDISPLAY2.setText(TVDISPLAY2.getText() + "9");
}
break;
case R.id.btadd: // add function
AddFunc(x); break;
case R.id.btclear: // clear
TVDISPLAY.setText(" ");
TVDISPLAY2.setText(" "); break;
case R.id.btback: // back
BackFunc(); break;
case R.id.btdivide: // divide
DevFunc(id); break;
case R.id.btdual: // Kefel
DualFuncdual(id); break;
case R.id.btequal: // Shave
EqualFunc(v); break;
case R.id.btpoint: // Nekuda
TVDISPLAY.setText(TVDISPLAY.getText() + ".");
Nekuda(); break;// will check if there is a point in the number
case R.id.btsub: // Sub
SubFunc(id);
Intent intent = new Intent(this,MainActivity.class);
intent = intent.putExtra("TVDISPLAY", TVDISPLAY.getText().toString());
}
}
private double EqualFunc(View v) {
// will get the id of the button that pressed
// check which button is pressed by id.
Button clickedButton = (Button) v;
switch (clickedButton.getId()) {
case R.id.btadd: // add
y=Double.parseDouble(TVDISPLAY2.getText().toString());
z=x+y;
TVDISPLAY.setVisibility(View.VISIBLE);
TVDISPLAY2.setVisibility(View.INVISIBLE);
TVDISPLAY.setText(Double.toString(z));
break;
case R.id.btsub: // sub
y=Double.parseDouble(TVDISPLAY2.getText().toString());
z=x-y;
TVDISPLAY.setVisibility(View.VISIBLE);
TVDISPLAY2.setVisibility(View.INVISIBLE);
TVDISPLAY.setText(Double.toString(z));
break;
case R.id.btdual: // dual
y=Double.parseDouble(TVDISPLAY2.getText().toString());
z=x*y;
TVDISPLAY.setVisibility(View.VISIBLE);
TVDISPLAY2.setVisibility(View.INVISIBLE);
TVDISPLAY.setText(Double.toString(z));
break;
case (R.id.btdivide): // divide
y=Double.parseDouble(TVDISPLAY2.getText().toString());
z=x/y;
TVDISPLAY.setVisibility(View.VISIBLE);
TVDISPLAY2.setVisibility(View.INVISIBLE);
TVDISPLAY.setText(Double.toString(z));
}
return z;
}
private double SubFunc(double x2) { // still empty
x=Double.parseDouble(TVDISPLAY.getText().toString());
TVDISPLAY.setVisibility(View.INVISIBLE);
TVDISPLAY2.setVisibility(View.VISIBLE);
return R.id.btsub;
}
private double DevFunc(double x2) {// still empty
x=Double.parseDouble(TVDISPLAY.getText().toString());
TVDISPLAY.setVisibility(View.INVISIBLE);
TVDISPLAY2.setVisibility(View.VISIBLE);
return R.id.btdivide;
}
private double DualFuncdual(double x2) {
x=Double.parseDouble(TVDISPLAY.getText().toString());
TVDISPLAY.setVisibility(View.INVISIBLE);
TVDISPLAY2.setVisibility(View.VISIBLE);
return R.id.btdual;
}
private double AddFunc( double x2) { // doesn't work.
x=Double.parseDouble(TVDISPLAY.getText().toString());
TVDISPLAY.setVisibility(View.INVISIBLE);
TVDISPLAY2.setVisibility(View.VISIBLE);
return x;
}
}
答案 0 :(得分:0)
尝试声明android:onClick =“btn1”或btn2或btn3等......
要获得他们的功能,您需要做的就是声明
public void btn1(查看v)
{
//在这里运作
}
答案 1 :(得分:0)
您可以尝试Mike发布的代码。 或者你可以这样做:
clickedButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
每次按下按钮'点击按钮'单击,方法' onClick(查看v)'被称为
或者你可以实现界面' OnClickListener'在你的课堂上,将听众注册到你的班级,如下所示:
clickedButton.setOnClickListener(this);
然后,关于方法' public void onClick(View v)'你可以这样做:
public void onClick(View v) {
switch(v.getId()){
case R.id.clickedButtonId :
//Do Stuff
break;
default:
break;
}
}