我下了一个记账程序要学习一下,发现看不懂代码啊,哪位高手给我瞧瞧,这个keyPad类是什么作用
代码如下:
public class KeyPad extends Activity implements OnClickListener {
private Button btnDisplay = null;
private Button btnOne = null;
private Button btnTwo = null;
private Button btnThree = null;
private Button btnFour = null;
private Button btnFive = null;
private Button btnSix = null;
private Button btnSeven = null;
private Button btnEight = null;
private Button btnNine = null;
private Button btnZero = null;
private Button btnDot = null;
private Button btnDelete = null;
private Button btnCancel = null;
private Button btnClean = null;
private Button btnDone = null;
private String value = "0";
private boolean isValueEmpty = false; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_digit_keypad);
btnDisplay = (Button) findViewById(R.id.display);
btnOne = (Button) findViewById(R.id.one);
btnTwo = (Button) findViewById(R.id.two);
btnThree = (Button) findViewById(R.id.three);
btnFour = (Button) findViewById(R.id.four);
btnFive = (Button) findViewById(R.id.five);
btnSix = (Button) findViewById(R.id.six);
btnSeven = (Button) findViewById(R.id.seven);
btnEight = (Button) findViewById(R.id.eight);
btnNine = (Button) findViewById(R.id.nine);
btnZero = (Button) findViewById(R.id.zero);
btnDot = (Button) findViewById(R.id.dot);
btnDelete = (Button) findViewById(R.id.delete);
btnCancel = (Button) findViewById(R.id.cancel);
btnClean = (Button) findViewById(R.id.clean);
btnDone = (Button) findViewById(R.id.done);
btnDisplay.setOnClickListener(this);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnThree.setOnClickListener(this);
btnFour.setOnClickListener(this);
btnFive.setOnClickListener(this);
btnSix.setOnClickListener(this);
btnSeven.setOnClickListener(this);
btnEight.setOnClickListener(this);
btnNine.setOnClickListener(this);
btnZero.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnCancel.setOnClickListener(this);
btnClean.setOnClickListener(this);
btnDone.setOnClickListener(this);
if(this.getIntent().hasExtra("value"))
{
value=this.getIntent().getStringExtra("value");
}
if(value==null || value.equals(""))
{
value="0";
isValueEmpty = true;
}
btnDisplay.setText(value); } /*
* (non-Javadoc)
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.display:
break;
case R.id.delete:
if (value.length() > 1) {
value = value.substring(0, value.length() - 1);
} else {
value = "0";
}
break;
case R.id.cancel:
this.setResult(RESULT_CANCELED);
this.finish();
break;
case R.id.clean:
value = "0";
break;
case R.id.done:
/*
* Bundle bundle = new Bundle(); bundle.putString("value", value);
* SendSMSActivity.this.setResult(RESULT_CANCELED,
* SendSMSActivity.this.getIntent().putExtras(bundle
*/
this.setResult(RESULT_OK, this.getIntent().putExtra("value", value));
this.finish();
break;
default:
int i = 0;
if(!isValueEmpty){
value = "0";
isValueEmpty = true;
}
if (v.getId() == R.id.dot) {
if (value.indexOf(".") > -1) {
break;
}
i = 1;
} else {
if (value.equals("0")) {
value = "";
}
}
if(value.indexOf(".")>-1 || i==1){
if (value.length() < 9) {
value = value + ((Button) v).getText();
}
}else{
if (value.length() < 6) {
value = value + ((Button) v).getText();
}
}
break;
}
// DecimalFormat df2 = new DecimalFormat("#####,##0.##");
if(value.indexOf(".")>-1 && value.indexOf(".")<value.length()-3)
{
value=value.substring(0,value.indexOf(".")+3);
}
//value=df2.format(Double.parseDouble(value));
btnDisplay.setText(value);//₩ }
}
Android
代码如下:
public class KeyPad extends Activity implements OnClickListener {
private Button btnDisplay = null;
private Button btnOne = null;
private Button btnTwo = null;
private Button btnThree = null;
private Button btnFour = null;
private Button btnFive = null;
private Button btnSix = null;
private Button btnSeven = null;
private Button btnEight = null;
private Button btnNine = null;
private Button btnZero = null;
private Button btnDot = null;
private Button btnDelete = null;
private Button btnCancel = null;
private Button btnClean = null;
private Button btnDone = null;
private String value = "0";
private boolean isValueEmpty = false; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_digit_keypad);
btnDisplay = (Button) findViewById(R.id.display);
btnOne = (Button) findViewById(R.id.one);
btnTwo = (Button) findViewById(R.id.two);
btnThree = (Button) findViewById(R.id.three);
btnFour = (Button) findViewById(R.id.four);
btnFive = (Button) findViewById(R.id.five);
btnSix = (Button) findViewById(R.id.six);
btnSeven = (Button) findViewById(R.id.seven);
btnEight = (Button) findViewById(R.id.eight);
btnNine = (Button) findViewById(R.id.nine);
btnZero = (Button) findViewById(R.id.zero);
btnDot = (Button) findViewById(R.id.dot);
btnDelete = (Button) findViewById(R.id.delete);
btnCancel = (Button) findViewById(R.id.cancel);
btnClean = (Button) findViewById(R.id.clean);
btnDone = (Button) findViewById(R.id.done);
btnDisplay.setOnClickListener(this);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnThree.setOnClickListener(this);
btnFour.setOnClickListener(this);
btnFive.setOnClickListener(this);
btnSix.setOnClickListener(this);
btnSeven.setOnClickListener(this);
btnEight.setOnClickListener(this);
btnNine.setOnClickListener(this);
btnZero.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnCancel.setOnClickListener(this);
btnClean.setOnClickListener(this);
btnDone.setOnClickListener(this);
if(this.getIntent().hasExtra("value"))
{
value=this.getIntent().getStringExtra("value");
}
if(value==null || value.equals(""))
{
value="0";
isValueEmpty = true;
}
btnDisplay.setText(value); } /*
* (non-Javadoc)
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.display:
break;
case R.id.delete:
if (value.length() > 1) {
value = value.substring(0, value.length() - 1);
} else {
value = "0";
}
break;
case R.id.cancel:
this.setResult(RESULT_CANCELED);
this.finish();
break;
case R.id.clean:
value = "0";
break;
case R.id.done:
/*
* Bundle bundle = new Bundle(); bundle.putString("value", value);
* SendSMSActivity.this.setResult(RESULT_CANCELED,
* SendSMSActivity.this.getIntent().putExtras(bundle
*/
this.setResult(RESULT_OK, this.getIntent().putExtra("value", value));
this.finish();
break;
default:
int i = 0;
if(!isValueEmpty){
value = "0";
isValueEmpty = true;
}
if (v.getId() == R.id.dot) {
if (value.indexOf(".") > -1) {
break;
}
i = 1;
} else {
if (value.equals("0")) {
value = "";
}
}
if(value.indexOf(".")>-1 || i==1){
if (value.length() < 9) {
value = value + ((Button) v).getText();
}
}else{
if (value.length() < 6) {
value = value + ((Button) v).getText();
}
}
break;
}
// DecimalFormat df2 = new DecimalFormat("#####,##0.##");
if(value.indexOf(".")>-1 && value.indexOf(".")<value.length()-3)
{
value=value.substring(0,value.indexOf(".")+3);
}
//value=df2.format(Double.parseDouble(value));
btnDisplay.setText(value);//₩ }
}
Android
解决方案 »
- 请问已经工作的各位大侠一个很基础的问题
- 关于handler,LOOPer,handerthread
- 在android开发中怎样使用百度翻译api接口
- Android连接指定无线网络
- windows平台软件移植到android平台的问题
- 在android上利用FFMPEG播放视频遇到的问题
- 自定义logo问题求帮助
- 在网上学的 状态栏 通知 的程序 自己做 总出错 求解决
- 求助:Unfortunately,Launcher has stopped
- 切换fragment时如何刷新fragment里的内容
- 都说用完sqlitedatabase要close,谁能举个资源泄露的例子见识见识?
- 【急】listview点击item动画效果
类似于一个计算器的界面,可以输入数字,删除,然后还有个done操作应该是给下面的activity传value把。