標籤:
對著書上敲了一波簡單的安卓應用,主要是通過年齡性別來給出婚姻建議,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下,就能出現開發人員選項。好吧。。。。賊神奇。。。。。
安卓簡單應用試水以及真機測試的問題解決