20170117 婚姻建議((EditText練習))
Android 練習的成果,婚姻建議(EditText練習)!
以下是程式碼
//婚姻建議(EditText練習)
//20170117劉彥廷
package com.example.user.mynwetest;
MainActivity.java
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 EditText mEdtSex,mEdtAge;
private TextView mTextR;
public 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);
mTextR = (TextView)findViewById(R.id.txtR);
mBtnOK = (Button)findViewById(R.id.btnOK);
mBtnOK.setOnClickListener(btnOKOnClick);
}
private View.OnClickListener btnOKOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
String strSex = mEdtSex.getText().toString();
//Integer 整數的意思
int iAge = Integer.parseInt(mEdtAge.getText().toString());
String strSug = getString(R.string.result);
if (strSex.equals(getString(R.string.sex_male)))
if (iAge < 28)
strSug += getString(R.string.sug_not_hurry);
else if (iAge > 33)
strSug += getString(R.string.sug_get_marry);
else
strSug += getString(R.string.sug_find_couple);
else if (iAge < 25)
strSug += getString(R.string.sug_not_hurry);
else if (iAge > 30)
strSug += getString(R.string.sug_get_marry);
else
strSug += getString(R.string.sug_find_couple);
mTextR.setText(strSug);
}
};
}
activvity_main.xml
<?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.user.mynwetest.MainActivity"
android:orientation="vertical">
<TextView
android:text="@string/sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Sex"
android:textSize="36sp"
android:textColorHighlight="@android:color/black" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="(請輸入性別)"
tools:textSize="24sp"
android:id="@+id/edtSex" />
<TextView
android:text="@string/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Age"
android:textSize="36sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="(請輸入年齡)"
android:textSize="24sp"
android:id="@+id/edtAge" />
<Button
android:text="@string/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnOK"
android:onClick="onClick" />
<TextView
android:text="@string/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtR"
android:textSize="24sp" />
</LinearLayout>
以下是程式碼
//婚姻建議(EditText練習)
//20170117劉彥廷
package com.example.user.mynwetest;
MainActivity.java
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 EditText mEdtSex,mEdtAge;
private TextView mTextR;
public 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);
mTextR = (TextView)findViewById(R.id.txtR);
mBtnOK = (Button)findViewById(R.id.btnOK);
mBtnOK.setOnClickListener(btnOKOnClick);
}
private View.OnClickListener btnOKOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
String strSex = mEdtSex.getText().toString();
//Integer 整數的意思
int iAge = Integer.parseInt(mEdtAge.getText().toString());
String strSug = getString(R.string.result);
if (strSex.equals(getString(R.string.sex_male)))
if (iAge < 28)
strSug += getString(R.string.sug_not_hurry);
else if (iAge > 33)
strSug += getString(R.string.sug_get_marry);
else
strSug += getString(R.string.sug_find_couple);
else if (iAge < 25)
strSug += getString(R.string.sug_not_hurry);
else if (iAge > 30)
strSug += getString(R.string.sug_get_marry);
else
strSug += getString(R.string.sug_find_couple);
mTextR.setText(strSug);
}
};
}
activvity_main.xml
<?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.user.mynwetest.MainActivity"
android:orientation="vertical">
<TextView
android:text="@string/sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Sex"
android:textSize="36sp"
android:textColorHighlight="@android:color/black" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="(請輸入性別)"
tools:textSize="24sp"
android:id="@+id/edtSex" />
<TextView
android:text="@string/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Age"
android:textSize="36sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:hint="(請輸入年齡)"
android:textSize="24sp"
android:id="@+id/edtAge" />
<Button
android:text="@string/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnOK"
android:onClick="onClick" />
<TextView
android:text="@string/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtR"
android:textSize="24sp" />
</LinearLayout>
strings.xml
<resources>
<string name="app_name">結婚建議</string>
<string name="sex">性別</string>
<string name="age">年齡</string>
<string name="sug_not_hurry">還不急</string>
<string name="result">建議</string>
<string name="sug_get_marry">趕快結婚</string>
<string name="sug_find_couple">開始找對象</string>
<string name="sex_male">男人</string>
<string name="btn_ok">確定</string>
</resources>
留言
張貼留言