android 常用組建案例

來源:互聯網
上載者:User

現在才接觸android,在看了一個案例之後照著攜了一個小例子:

第一個頁面MainActivity:

package com.hoperun.activity;

import java.text.DecimalFormat;

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.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 
 private Button button_calc;
 private EditText field_height;
 private EditText field_weight;
 private TextView view_result;
 private TextView view_suggest;
 
 protected static final int MENU_ABOUT = Menu.FIRST;
 protected static final int MENU_QUIT = Menu.FIRST + 1;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        findViews();
        button_calc.setOnClickListener(calcBMI);
       
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     // TODO Auto-generated method stub
     menu.add(0, MENU_ABOUT, 0, "關於");
     menu.add(0, MENU_QUIT, 0, "退出");
     
     return super.onCreateOptionsMenu(menu);
    }
   
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
     switch (item.getItemId()) {
      case MENU_ABOUT:
       openOptionsDialog();
       break;
      case MENU_QUIT:
       finish();
       break;
     }
     return super.onMenuItemSelected(featureId, item);
    }
   
    private void findViews() {
     button_calc = (Button) findViewById(R.id.bmiButton);
     field_height = (EditText) findViewById(R.id.height);
     field_weight = (EditText) findViewById(R.id.weight);
     view_result = (TextView) findViewById(R.id.result);
     view_suggest = (TextView) findViewById(R.id.suggest);
    }
   
    private OnClickListener calcBMI = new OnClickListener(){

  private AlertDialog show;

  public void onClick(View v) {
   DecimalFormat nf = new DecimalFormat("0.00");
   double height = Double.parseDouble(field_height.getText().toString()) / 100;
   double weight = Double.parseDouble(field_weight.getText().toString());
   double bmi = weight / (height * height);
   view_result.setText("you bmi is: " + nf.format(bmi));
   
   if (bmi > 25) {
    view_suggest.setText(R.string.suggest_heavy);
    
   } else if (bmi < 20) {
    view_suggest.setText(R.string.suggest_light);
   } else {
    view_suggest.setText(R.string.suggest_average);
   }
//   openOptionsDialog();
  }
  
    };
   
    //建立對話方塊
    private void openOptionsDialog() {
//   Toast.makeText(MainActivity.this, "確定", Toast.LENGTH_LONG).show();
     new AlertDialog.Builder(MainActivity.this).setTitle(R.string.about_title)
     .setMessage(R.string.about_msg).setPositiveButton(R.string.about_positive,
       new DialogInterface.OnClickListener() {
      
      public void onClick(DialogInterface dialog,
        int which) {
      }
      
     }
     ).show();
    }
   
}

寫完後在AndroidManifest.xml中進行註冊。


string.xml中的代碼為:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">BMI</string>
    <string name="height">升高(cm)</string>
    <string name="weight">體重(kg)</string>
    <string name="suggest_heavy">太重了</string>
    <string name="suggest_average">平均</string>
    <string name="suggest_light">太輕了</string>
    <string name="about_title">關於android bmi</string>
    <string name="about_msg">android bmi calculation</string>
    <string name="about_positive">確定</string>
</resources>


在介面描述檔案中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/height"
     />
 <EditText
  android:id="@+id/height"
  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/weight"
  />
 <EditText
  android:id="@+id/weight"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:numeric="integer"
  android:text=""
  />
  
 <Button
  android:id="@+id/bmiButton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="計算BMI值"
  />
 <TextView
  android:id="@+id/result"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text=""
  />
 <TextView
  android:id="@+id/suggest"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text=""
  />
</LinearLayout>

主要的內容在這裡就算是完成了,下來就可以運行android project了


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.