对着书上敲了一波简单的安卓应用,主要是通过年龄性别来给出婚姻建议,po一下代码;
.java文件:
package com.example.admin.experiment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private Button btnDoSug;
    private EditText edtAge,edtSex;
    private TextView txtResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupViewComponent();
    }
    private void setupViewComponent() {
        btnDoSug= (Button) findViewById(R.id.btnDoSug);
        edtAge= (EditText) findViewById(R.id.edtAge);
        edtSex= (EditText) findViewById(R.id.edtSex);
        txtResult= (TextView) findViewById(R.id.txtResult);
        btnDoSug.setOnClickListener(btnDoSugOnClick);
    }
    private Button.OnClickListener btnDoSugOnClick= new Button.OnClickListener() {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        @Override
        public void onClick(View v) {
            String Sex=edtSex.getText().toString();
            int age=Integer.parseInt(edtAge.getText().toString());
            String result="结果";
            if(Sex.equals("男"))
                if (age<28)
                    result+="还不急";
                else if (age>33)
                    result+="赶快结婚";
                else
                    result+="开始找对象";
            else
                if (age<25)
                    result+="还不急";
                else if (age>30)
                    result+="赶快结婚";
                else
                    result+="开始找对象";
            txtResult.setText(result);
        }
    };
}
.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="性别"
        android:id="@+id/textView" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edtSex"
        android:text=""
        android:inputType="text"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="年龄"/>
    <EditText
        android:id="@+id/edtAge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:text=""/>
    <Button
        android:id="@+id/btnDoSug"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="建议"/>
    <TextView
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="结果:"/>
</LinearLayout>
重要的点
getText()得到文本;
Integer.pareseInt()转换成int型;
toString()转换成字符串;
setText()设定文本;
A.equals(B)判断是否相等;
真机测试的时候,按照以往应该是设定USb调试,几年前记得手机上还是有这个选项的,今天找半天没找到,上网上查到解决方法:
在本机信息上找到版本号,然后嗯3下,然后再连着嗯4下,就能出现开发者选项。好吧。。。。贼神奇。。。。。
原文:http://www.cnblogs.com/overwatcher/p/5917480.html