安卓簡單應用試水以及真機測試的問題解決

來源:互聯網
上載者:User

標籤:

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

 

安卓簡單應用試水以及真機測試的問題解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.