MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.view.View;
import android.widget.Button; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bsub,bmul,bdiv,bdot,bans,bclear,b_bs; TextView ans; String a; int lenght; double var1,var2; boolean add,sub,mul,div; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.btn1);
b2=(Button)findViewById(R.id.btn_2);
b3=(Button)findViewById(R.id.btn_3);
b4=(Button)findViewById(R.id.btn4);
b5=(Button)findViewById(R.id.btn_5);
b6=(Button)findViewById(R.id.btn_6);
b7=(Button)findViewById(R.id.btn_7);
b8=(Button)findViewById(R.id.btn_8);
b9=(Button)findViewById(R.id.btn_9);
b0=(Button)findViewById(R.id.btn_0);
badd=(Button)findViewById(R.id.btn_Add);
bsub=(Button)findViewById(R.id.btn_Sub);
bmul=(Button)findViewById(R.id.btn_Multiply);
bdiv=(Button)findViewById(R.id.btn_div);
bdot=(Button)findViewById(R.id.btn_dot);
bans=(Button)findViewById(R.id.btn_equal);
bclear=(Button)findViewById(R.id.btn_Clear);
ans=(TextView)findViewById(R.id.Answer);
b_bs=(Button) findViewById(R.id.btn_bs);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText("");
ans.setText(ans.getText().toString()+"1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "9");
}
});
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ "0");
}
});
bdot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText(ans.getText()+ ".");//this is the place where i need help }
});
b_bs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str=ans.getText().toString();
if (str.length() >1 ) {
str = str.substring(0, str.length() - 1);
ans.setText(str);
}
else if (str.length() <=1 ) {
ans.setText("0");
}
}
});
badd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
var1=Double.parseDouble(ans.getText()+"");
add=true;
ans.setText(null);
}
});
bsub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
var1=Double.parseDouble(ans.getText()+"");
sub=true;
ans.setText(null);
}
});
bmul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
var1=Double.parseDouble(ans.getText()+"");
mul=true;
ans.setText(null);
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
var1=Double.parseDouble(ans.getText()+"");
div=true;
ans.setText(null);
}
});
bans.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
var2=Double.parseDouble(ans.getText()+"");
if(add==true){
ans.setText(var1+var2+"");
add=false;
}
if(sub==true){
ans.setText(var1-var2+"");
sub=false;
}
if(mul==true){
ans.setText(var1*var2+"");
mul=false;
}
if(div==true){
ans.setText(var1/var2+"");
div=false;
}
}
});
bclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ans.setText("0");
// a=ans.length();
}
});
} }
我已经制作了简单的Android计算器,现在我有一些问题可以解决这些问题
答案 0 :(得分:1)
1:仅显示一次点
接受一个boolean
变量
boolean isDotPressed = false;
然后单击onclick时,只需检查
bdot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!isDotPressed){
ans.setText(ans.getText()+ ".");//this is the place where i need help
isDotPressed = true;
}
}
});
b_bs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isDotPressed = false;
String str=ans.getText().toString();
if (str.length() >1 ) {
str = str.substring(0, str.length() - 1);
ans.setText(str);
}
else if (str.length() <=1 ) {
ans.setText("0");
}
}
});
2:默认值0
将编辑文本的默认值设置为“ 0”
将此行放入xml文件中的EditText中
android:hint="0"