9-第一个app项目
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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.administrator.married.MainActivity" android:orientation="vertical" android:weightSum="1"> <TextView android:text="性别:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40dp" android:textColor="@android:color/black" android:id="@+id/sex" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/edtSex" android:hint="(输入性别)" android:singleLine="false" /> <TextView android:text="年龄:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40dp" android:textColor="@android:color/black" android:id="@+id/age" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/edtAge" android:hint="(输入年龄)" /> <Button android:text="确定" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnOk" android:layout_gravity="center_horizontal" android:textSize="40dp" android:elevation="24dp" /> <TextView android:text="@string/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40dp" android:textColor="@android:color/black"
package com.example.administrator.married; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.*; public class MainActivity extends AppCompatActivity { EditText mEdtSex,mEdtAge; TextView mTxtR; Button mBtnOk; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEdtSex = (EditText)findViewById(R.id.edtSex); mEdtAge = (EditText)findViewById(R.id.edtAge); mTxtR = (TextView)findViewById(R.id.txtR); mBtnOk = (Button)findViewById(R.id.btnOk); mBtnOk.setOnClickListener(btnOkOnClick); } private View.OnClickListener btnOkOnClick = new View.OnClickListener(){ public void onClick(View v){ String strSex=mEdtSex.getText().toString(); int iAge=Integer.parseInt(mEdtAge.getText().toString()); String strSug = getString(R.string.result); if(strSex.equals("男")){ if(iAge<28){ strSug+="池哥告诉你:还不急"; } else if(iAge>33){ strSug+="池哥告诉你:赶快结婚!"; } else{ strSug+="池哥告诉你:开始找对象!"; } } else{ if(iAge<25){ strSug+="池哥告诉你:还不急"; } else if(iAge>30){ strSug+="池哥告诉你:赶快结婚!"; } else{ strSug+="池哥告诉你:开始找对象!"; } } mTxtR.setText(strSug); } }; }
android:id="@+id/txtR" /> </LinearLayout>