activity_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/promptSex"/> <EditText android:id="@+id/edtSex" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/promptAge"/> <EditText android:id="@+id/edtAge" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:text="" /> <Button android:id="@+id/btnDoSug" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/promptBtnDoSug" /> <TextView android:id="@+id/txtResult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/sugResult" /> </LinearLayout> |
mian:package com.example.administrator.excample2; 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 main extends AppCompatActivity { private Button btnDoSug; private EditText edtSex,edtAge; private TextView txtResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stupViewComponent(); } private void stupViewComponent(){ 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() { @Override public void onClick(View v) { String strSex=edtSex.getText().toString(); int iAge=Integer.parseInt(edtAge.getText().toString()); String strSug=getString(R.string.sugResult); if(strSex.equals(getString(R.string.sexMale))) if(iAge<28) strSug+=getString(R.string.sugNotHurry); else if (iAge>33) strSug+=getString(R.string.sugGetMarried); else strSug+=getString(R.string.sugFindCounple); else if (iAge<25) strSug+=getString(R.string.sugNotHurry); else if (iAge>30) strSug+=getString(R.string.sugGetMarried); else strSug+=getString(R.string.sugFindCounple); txtResult.setText(strSug); } }; } |
strings.xml:<?xml version="1.0" encoding="utf-8" ?> <resources> <string name="app_name">婚姻建议</string> <string name="promptSex">性别:</string> <string name="promptAge">年龄:</string> <string name="promptBtnDoSug">婚姻建议</string> <string name="sugResult">结果:</string> <string name="sugNotHurry">还不急。</string> <string name="sugGetMarried">赶快结婚!</string> <string name="sugFindCounple">开始找对象</string> <string name="sexMale">男</string> </resources> |
出现问题:
1、在AVD中输入文字的时候没有中文输入
2、将app-debug.apk安装在手机上的时候,输入数字点击婚姻建议的时候,直接关闭APP,运行结果与在AVD中的运行不一致,总的说是失败的。
参考书籍代码