第一部分 准备知识 01 Android整体接触

A 磨刀不误砍柴工——开发工具比较

搜索关键字

Intellij Idea;Eclipse


B 万丈高楼平地起——从开发到打包

搜索关键字

Android组件;布局;编程规范


小项目:

本例实现一个简易表单,通过一个activity收集用户输入数据,利用bundle进行存储。然后利用intent将界面跳转到另一个activity。

(注:两个RadioButton应该放于RadioGroup中,方向为水平方向)

完整代码

MainActivity

package com.example.note01bdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

	private RadioGroup radioGroup;
	private EditText editText;
	private Button button;
	String sex = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editText = (EditText) findViewById(R.id.et_height);
				// 将输入的字符串转换为双精度浮点型数字
				double height = Double.parseDouble(editText.getText()
						.toString());

				radioGroup = (RadioGroup) findViewById(R.id.rg_sex);
				radioGroup
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(RadioGroup group,
									int checkedId) {
								// TODO Auto-generated method stub

								if (checkedId == R.id.rb_male) {
									sex = "male";
								} else {
									sex = "female";
								}
							}
						});

				Intent intent = new Intent(MainActivity.this,
						ResultActivity.class);
				Bundle bundle = new Bundle();
				bundle.putDouble("height", height);
				bundle.putString("sex", sex);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
	}

}

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.note01bdemo.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="计算你的标准体重"
        android:textSize="24sp" />

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:text="性别:" />

        <RadioGroup
            android:id="@+id/rg_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男性" />

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女性" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_below="@id/layout1"
        android:layout_marginTop="25dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="身高:" />

        <EditText
            android:id="@+id/et_height"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="cm" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:gravity="center"
        android:text="计算" />

</RelativeLayout>

ResultActivity

package com.example.note01bdemo;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result_layout);
		Bundle bundle = this.getIntent().getExtras();
		double height = bundle.getDouble("height");
		String sex = bundle.getString("sex");
		String sexText = "";
		if (sex.equals("male")) {
			sexText = "男性";
		} else {
			sexText = "女性";
		}
		String weight = this.getWeight(sex, height);

		TextView textView = (TextView) findViewById(R.id.tv_result);
		textView.setText("你是一位" + sexText + "\n你的身高是" + height + "公分\n你的标准体重是"
				+ weight + "公斤");
	}

	// 四舍五入的算法
	private String format(double num) {
		NumberFormat formatter = new DecimalFormat("0.00");
		String s = formatter.format(num);
		return s;
	}

	private String getWeight(String sex, double height) {
		// TODO Auto-generated method stub
		String weight = "";
		if (sex.equals("male")) {
			weight = format((height - 80) * 0.7);
		} else {
			weight = format((height - 80) * 0.6);
		}
		return weight;
	}
}

result_layout

<?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" >

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />

</LinearLayout>

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.note01bdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ResultActivity" >
        </activity>
    </application>

</manifest>

预览图

第一部分 准备知识 01 Android整体接触_第1张图片第一部分 准备知识 01 Android整体接触_第2张图片

C 谈钱不伤感情——把APP放上市场

搜索关键字

Android Market;eoemarket;Android反编译;Android广告


你可能感兴趣的:(第一部分 准备知识 01 Android整体接触)