本範例將製作一個計算機,利用簡單的按鈕組合、不同的按鈕事件,來處理輸入的數字,輸入數字及點擊按鈕不同,結果也隨之不同。本範例為了使輸入小數時也可以使用,將數字都設為float(浮點數),以避免例外錯誤發生。
小試身手小範例
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CalculatorExample extends Activity {
/** Called when the activity is first created. */
public Button mButton01;
public Button mButton02;
public Button mButton03;
public Button mButton04;
public EditText mEditText01;
public EditText mEditText02;
public TextView mTextView01;
public TextView mTextView02;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mTextView01 = (TextView)findViewById(R.id.TextView01);
mTextView02 = (TextView)findViewById(R.id.TextView02);
mEditText01 = (EditText)findViewById(R.id.EditText01);
mEditText02 = (EditText)findViewById(R.id.EditText02);
mButton01 = (Button)findViewById(R.id.ButtonAdd);
mButton02 = (Button)findViewById(R.id.ButtonPlus);
mButton03 = (Button)findViewById(R.id.ButtonMultiplied);
mButton04 = (Button)findViewById(R.id.ButtonDivided);
mButton01.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
mTextView01.setText("+");
String strRet = Float.toString(Float.parseFloat(mEditText01.getText().toString())
+Float.parseFloat(mEditText02.getText().toString()));
mTextView02.setText(strRet);
}
});
mButton02.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
mTextView01.setText("-");
String strRet = Float.toString(Float.parseFloat(mEditText01.getText().toString())
-Float.parseFloat(mEditText02.getText().toString()));
mTextView02.setText(strRet);
}
});
mButton03.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
mTextView01.setText("*");
String strRet = Float.toString(Float.parseFloat(mEditText01.getText().toString())
*Float.parseFloat(mEditText02.getText().toString()));
mTextView02.setText(strRet);
}
});
mButton04.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
mTextView01.setText("/");
String strRet = Float.toString(Float.parseFloat(mEditText01.getText().toString())
/Float.parseFloat(mEditText02.getText().toString()));
mTextView02.setText(strRet);
}
});
}
}
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"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/EditText01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/TextView01"
android:layout_marginRight="10sp"
android:layout_marginLeft="10sp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/EditText02"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" = "
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10sp"
android:layout_marginLeft="10sp"
android:id="@+id/TextView02"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
>
<Button
android:layout_width="60sp"
android:layout_height="wrap_content"
android:id="@+id/ButtonAdd"
android:text=" + "
android:layout_marginRight="10sp"
/>
<Button
android:layout_width="60sp"
android:layout_height="wrap_content"
android:id="@+id/ButtonPlus"
android:text=" - "
android:layout_marginRight="10sp"
/>
<Button
android:layout_width="60sp"
android:layout_height="wrap_content"
android:id="@+id/ButtonMultiplied"
android:text=" * "
android:layout_marginRight="10sp"
/>
<Button
android:layout_width="60sp"
android:layout_height="wrap_content"
android:id="@+id/ButtonDivided"
android:text=" / "
/>
</LinearLayout>
</LinearLayout>
androidSDK0096
留言列表