格瑞克愛(ài)鮮荔枝
2017-03-12 19:54:34
/*下面是Android-src-HeightCalculator的程序*/
package?com.demo.prH2;???
import?android.app.Activity;?
import?android.app.AlertDialog;
import?android.content.DialogInterface;
import?android.os.Bundle;?
import?android.view.Menu;
import?android.view.MenuItem;
import?android.view.View;???
import?android.view.View.OnClickListener;
import?android.widget.Button;
import?android.widget.CheckBox;?
import?android.widget.*;
import?android.widget.EditText;
import?android.widget.TextView;
/*import?java.text.DecimalFormat;???
import?java.text.NumberFormat;???
import?android.widget.RadioButton;???
import?android.widget.Toast;?????
/*??
*?@author?chengkaige?*?
*/??
public?class?HeightCaculatorActivity?extends?Activity?{???
/**?Called?when?the?activity?is?first?created.?*/??
????private?Button?calculatorButton;?????
????private?EditText?weightEditText;?
????private?CheckBox?manCheckBox;
????private?CheckBox?womanCheckBox;
????private?TextView?resultTextView;??????
?????
????@Override????
????public?void?onCreate(Bundle?savedInstanceState)?{?????
????????super.onCreate(savedInstanceState);?????
????????setContentView(R.layout.main);????
????????//調(diào)用創(chuàng)建視圖的函數(shù)???
????????calculatorButton?=(Button)findViewById(R.id.calculator);
????????weightEditText=(EditText)findViewById(R.id.weight);
????????manCheckBox=(CheckBox)findViewById(R.id.man);
???????womanCheckBox?=?(CheckBox)findViewById(R.id.woman);
????????resultTextView=(TextView)findViewById(R.id.result);??
???}?????
????private?void?onStar()?{?????
???????super.onStart();??
???????registerEvent();
????}?????
????
????private?void?registerEvent()?{?????
???? calculatorButton.setOnClickListener(new?OnClickListener(){?????
????????@Override????
????????public?void?onClick(View?v)?{?????
???????????if(!weightEditText.getText().toString().trim().equals("")){
???????? MenuItem?womanCheckBox?=?null;
if(manCheckBox.isChecked()||womanCheckBox.isChecked())???
???????? {
???????? Double?weight=Double.parseDouble(weightEditText.getText().toString());
???????? StringBuffer?sb=new?StringBuffer();
???????? sb.append("------評(píng)估結(jié)果-----\n");
???????? if(manCheckBox.isChecked()){
???????? sb.append("男性標(biāo)準(zhǔn)身高:");
???????? double?result=evaluateHeight(weight,"男");
???????? sb.append((int)result+"厘米");
???????? }
???????? if(womanCheckBox.isChecked()){
???????? sb.append("女性標(biāo)準(zhǔn)身高:");
???????? double?result=evaluateHeight(weight,"女");
???????? sb.append((int)result+"厘米");
???????? }
???????? resultTextView.setText(sb.toString());
???????? }else
???????? {
???????? showMessage("請(qǐng)選擇性別呀!");
???????? }
???????}else{
???? ???showMessage("請(qǐng)輸入體重!");
???????}???
???????????}
????????????
???? });??
????}
????private?double?evaluateHeight(double?weight,String?sex)
????{
???? double?height;
???? if(sex=="男"){
???? height=170-(62-weight)/0.6;}
???? else{
???? height=158-(52-weight)/0.5;}
???? return?height;}
????
????
????private?void?showMessage(String?message)
????{
???? AlertDialog?alert=new?AlertDialog.Builder(this).create();
???? alert.setTitle("系統(tǒng)消息");
???? alert.setMessage(message);
???? alert.setButton("確定",new?android.content.DialogInterface.OnClickListener(){
???? public?void?onClick(DialogInterface?dialog,int?whichButton){}
???? });
???? alert.show();
????}
????
????public?boolean?onCreateOptionMenu(Menu?menu){
???? menu.add(Menu.NONE,1,Menu.NONE,"退出");
???? return?super.onCreateOptionsMenu(menu);
????}
????
????public?boolean?onOptionItemSelected(MenuItem?item){
???? switch(item.getItemId()){
???? case?1:
???? finish();
???? break;
???? }
???? return?super.onOptionsItemSelected(item);
????}底下是框架layout里面的程序,沒(méi)有報(bào)錯(cuò),就是上面的main一直報(bào)錯(cuò),剛學(xué)安卓可能問(wèn)題很簡(jiǎn)單可是很笨就是調(diào)不出來(lái),啊啊啊??!求大佬指導(dǎo)一波吖。。。。<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"? ? android:layout_width="match_parent"? ? android:layout_height="match_parent"? ? android:orientation="vertical" >? ??? ?<LinearLayout? ? ? ? android:id="@+id/linearLayout1"? ? ? ? android:layout_width="fill_parent"? ? ? ? android:layout_height="wrap_content"? ? ? ? android:layout_marginTop="20.0dp"? ? ? ? android:gravity="center_horizontal"? ? ? ? android:orientation="horizontal" >? ? ? ??? ? ? ? <TextView? ? ? ? ? ? android:layout_width="wrap_content"? ? ? ? ? ? android:layout_height="wrap_content"? ? ? ? ? ? android:gravity="center_horizontal"? ? ? ? ? ? android:text="個(gè)人標(biāo)準(zhǔn)身高計(jì)算器"? ? ? ? ? ? android:textSize="22.0dip"? ? ? ? ? ? android:textStyle="bold" />? ? </LinearLayout>? ? <LinearLayout? ? ? ? android:layout_width="wrap_content"? ? ? ? android:layout_height="wrap_content"? ? ? ? android:layout_marginTop="10dp"? ? ? ? android:orientation="horizontal" >? ? ? ? <TextView? ? ? ? ? ? android:layout_width="120.0dip"? ? ? ? ? ? android:layout_height="wrap_content"? ? ? ? ? ? android:layout_marginLeft="5.0dip"? ? ? ? ? ? android:text="請(qǐng)輸入你的體重:" />? ? ? ? <EditText? ? ? ? ? ? android:id="@+id/weight"? ? ? ? ? ? android:layout_width="150.0dip"? ? ? ? ? ? android:layout_height="wrap_content"? ? ? ? ? ? android:inputType="number" />? ? ? ? <TextView? ? ? ? ? ? android:layout_width="wrap_content"? ? ? ? ? ? android:layout_height="wrap_content"? ? ? ? ? ? android:text="kg" />? ? </LinearLayout>? ? <LinearLayout? ? ? ? android:layout_width="match_parent"? ? ? ? android:layout_height="wrap_content"?? ? ? ? android:orientation="horizontal">? ? ? ? ?<TextView?? ? ? ? android:id="@+id/textView1"? ? ? ? android:layout_width="wrap_content"? ? ? ? android:layout_height="wrap_content"? ? ? ? android:text="請(qǐng)選擇你的性別"? ? ? ? android:layout_marginLeft="5.0dip" />? ? ? ? ?<CheckBox? ? ? ? ? ? ?android:id="@+id/man"? ? ? ? ? ? ?android:layout_width="wrap_content"? ? ? ? ? ? ?android:layout_height="wrap_content"? ? ? ? ? ? ?android:text="男" /> ? ? ? ? ?<CheckBox? ? ? ? ? ? ?android:id="@+id/woman"? ? ? ? ? ? ?android:layout_width="wrap_content"? ? ? ? ? ? ?android:layout_height="wrap_content"? ? ? ? ? ? ?android:text="女" />? ? </LinearLayout>? ? <LinearLayout? ? ? ? android:layout_width="match_parent"? ? ? ? android:layout_height="wrap_content"? ? ? ? android:orientation="horizontal"?? ? ? ? android:gravity="center_horizontal">? ? ? ? ? ?<Button? ? ? ? android:id="@+id/calculator"? ? ? ? android:layout_marginTop="20dip"? ? ? ? android:layout_width="150dip"? ? ? ? android:layout_height="wrap_content"? ? ? ? android:text="運(yùn)算" />? ? </LinearLayout>? ? <LinearLayout? ? ? ? android:layout_width="match_parent"? ? ? ? android:layout_height="wrap_content"?? ? ? ? ?android:orientation="horizontal"?? ? ? ? android:gravity="center_horizontal">? ? ? ? <TextView? ? ? ? ? ? android:id="@+id/result"? ? ? ? ? ? android:layout_width="wrap_content"? ? ? ? ? ? android:layout_height="wrap_content"? ? ? ? ? ? android:layout_marginTop="10dip"? ? ? ? ? ? ?/>? ? </LinearLayout>? ? <LinearLayout? ? ? ? android:layout_width="fill_parent"? ? ? ? android:layout_height="fill_parent"? ? ? ? android:orientation="vertical" >? ? </LinearLayout></LinearLayout>
- 2 回答
- 0 關(guān)注
- 2931 瀏覽
添加回答
舉報(bào)
0/150
提交
取消