發新話題
打印

[分享]簡單的android程式

[分享]簡單的android程式

之前介紹過 button ,textview 等等基本元件
今天來點實用的:   簡易的計算機

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculate_layout);
//declare the tmp
cal_number=new float[]{0,0,0,0,0,0,0,0,0,0};
//textView1
display_text=(TextView)findViewById(R.id.textView1);
display_text.setTextSize(20);
display_text.setText("0");
//num 1
num_Button1=(Button)findViewById(R.id.button1);
num_Button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if((last_op==1)||((Float.parseFloat(display_text.getText().toString()))==0) )
{
display_text.setText("1");
last_op=0;
}else
{
display_text.setText(display_text.getText()+"1");
}
}
});
//num 1--end
//op: +
op_button1=(Button)findViewById(R.id.button11);
op_button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//display_text.setText(display_text.getText()+"9");
if((last_op!=1)&&((Float.parseFloat(display_text.getText().toString()))!=0)  )
{
cal_number[number_count]=Float.parseFloat(display_text.getText().toString());
Log.d(tag,""+cal_number[number_count]);
number_count++;
last_op=1;
}
}
});
//op: +--end
//op: =
op_button5=(Button)findViewById(R.id.button16);
op_button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cal_number[number_count]=Float.parseFloat(display_text.getText().toString());
Log.d(tag,""+cal_number[number_count]);
ans=cal_number[number_count-1]+cal_number[number_count];
Log.d(tag,""+ans);
display_text.setText(Float.toString(ans));
number_count++;
}
});
//op: = --end
}



上圖就是:  1+1=2的範例
說穿了
這個工具的元件只有TextView   與  Button
其餘的就看個人怎麼寫了,  上方的sample 只有實現 1 的加法
<例如>  1+1    , 1+11,   111+111
做為一個例子    不需要太多    重點就在這一小段而已
其餘的運算    就不用再提    是這類似的CODE再多寫幾次就行了
有興趣的人可以下載來研究

簡易計算機

TOP

更精緻的話可取道整數位喔

TOP

受用良多,謝謝大大。。。。

TOP

發新話題