Android学习笔记(三)

一个Android计算器软件小例子解析:

main.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/first_number"
    />
<EditText android:id="@+id/first"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:numeric="integer"
     android:text=""
/>
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/second_number"
    />
<EditText android:id="@+id/second"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:inputType="number"
     android:text=""
/>
<Button android:id="@+id/button"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/button"
    />
<TextView android:id="@+id/result"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=""
   />
</LinearLayout>

 

strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="first_number">输入第一个数:</string>
    <string name="second_number">输入第二个数:</string>
    <string name="button">计算:</string>
    <string name="calcresult">你的计算结果是:</string>
    <string name="app_name">Calc</string>
    <string name="about">关于...</string>
    <string name="message">Android Calc</string>
    <string name="label_ok">确认</string>
</resources>

Calc.java

public class Calc extends Activity {
    /** Called when the activity is first created. */
  @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         findView();
         //Listen for button clicks
         setListener();
     }
  private EditText fieldfirst;
  private EditText fieldsecond;
  private TextView result;
  private Button button;
  private DecimalFormat df;
  private double first;
  private double second;
  private double sum;
  private void setListener() {
   button.setOnClickListener(CalcB);
  
 }
 
  private void findView() {
   fieldfirst=(EditText)findViewById(R.id.first);
   fieldsecond=(EditText)findViewById(R.id.second);
   button=(Button)findViewById(R.id.button);
  
 }
 private OnClickListener CalcB=new OnClickListener() {
  @Override
  public void onClick(View v) {
   df=new DecimalFormat("0.00");
   first=Double.parseDouble(fieldfirst.getText().toString());
   second=Double.parseDouble(fieldsecond.getText().toString());
   sum=first+second;
   result=(TextView)findViewById(R.id.result);
   //result.setText("你的计算结果是:"+df.format(sum));
   result.setText(getText(R.string.calcresult)+df.format(sum)); //R.string.calcresult不能和数字连接
   openOptionsDialog();
  }
  
 };
 //计算结果后,弹出对话框(通过AlertDialog实现的)
 private void openOptionsDialog(){
  /*
   * 实现弹出对话框第一种方法
   */
//  new AlertDialog.Builder(Calc.this)
//  .setTitle(R.string.about)
//  .setMessage(R.string.message)
//  .setPositiveButton(R.string.label_ok,new DialogInterface.OnClickListener() {
//   @Override
//   public void onClick(DialogInterface dialog, int which) {
//   }
//  })
//  .show();

  /*
   * 实现弹出对话框第二种方法
   */
  Toast.makeText(Calc.this,R.string.message, Toast.LENGTH_SHORT).show();
 }
 
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值