一个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();
}
}