通过实例学Android应用开发01
通過實例學安卓開發
- 序
- 實例
- 題目
- 程序結果展示界面
- 涉及到的知識點
- 布局
- 文本框控件
- 按鈕控件
- 其他控件
- 實現過程
- 源碼
- 總結
序
???????本人是十分厭煩理論知識的學習,咱學了十二年來到了高校,仍然還要學理論知識,但不同的是,時間上更充裕了,我想怎么學就怎么學。填報志愿的時候,我沒想那么多,就是覺得自己挺喜歡敲鍵盤的,所以最后我的志愿單上一溜圈的都是和計算機有關的,慶幸的是,我堅持了三年,我依舊不后悔且熱愛我的專業,盡管我的數學不是那么好。更要命的是,我好像還是對實驗操作類的更感興趣,比如說什么數字電子什么與或非門啥的,雖然又忘的差不多了。但我還是記得做完實驗后看到電子顯示器成功顯示數據時的開心。咳咳,以后還是會走軟件開發方向的應該吧。
???????本學期開設了移動開發,這個課程挺簡單的,老師布置的課堂任務都是書本上有的,不就是比打字速度嘛,除開機房設備的影響,我都是唰唰幾下就搞完了,所以我覺得沒勁。但期末還有程序設計及考試,我決定還是自己好好看書學一下基礎知識。然后抽空學下鴻蒙系統的開發。
???????因此,就有你們看到的這篇文了,我給自己看的,你們隨意~
實例
題目
使用文本框和按鈕控件制作用戶登錄界面。
程序結果展示界面
涉及到的知識點
布局
線性布局LinearLayout、表格布局TableLayout、相對布局RelativeLayout、
層布局FrameLayout、絕對布局AbsoluteLayout、網格布局GridLayout。
文本框控件
文本框控件包括TextView和EditView。前者是用來顯示字符的控件,后者是用來輸入和編輯字符的控件。
按鈕控件
Button控件通過setOnClickListener()方法設置單擊事件監聽器。
其他控件
顯示圖像控件ImageView,必填屬性:android:src指定圖像來源。
快顯信息與類Toast:Toast可以看作是一個會自動消失的信息框。
實現過程
源碼
布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><!--orientation 是為了給線性布局LinearLayout指定布局方向的,vertical是垂直布局,horizontal是水平布局--><!--layout_width 設置的是當前組件占屏寬度layout_height 設置的是當前組件所占高度它倆的值都有match_parent(與父組件寬度一致,但當前此組件為頂級【非專業術語,自稱的】組件,默認寬度為顯示屏寬度)和wrap_content(與內容一致,適配組件自身大小)--><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="100sp"android:text="用戶登錄"android:textSize="22sp"/><!--layout_marginTop: 距頂部組件位置大小只要帶gravity的都表示對其方式,但layout_gravity表示的是當前組件的對齊方式,單獨的gravity則表示組件中內容的對齊方式--><!--text 是組件顯示內容,textSize是文本顯示內容大小,sp是單位,還有好多可以看idea提示idea很強大,都不需要記憶,隨心就好--><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="100sp"android:layout_marginLeft="50sp"android:orientation="horizontal"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="用戶名:" android:textSize="20sp"/><EditText android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content"android:hint="請輸入用戶名" android:textSize="20sp" android:width="235sp" /><!--@+id 在res/values/strings添加一個新的idR.id中也會自動生成,盡管idea中我沒找著,肯定是封裝了的--><!--hint 就是提示詞,用戶輸入內容后會隱藏起來--></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20sp"android:layout_marginLeft="50sp"android:orientation="horizontal"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="密 碼:" android:textSize="20sp"/><EditText android:id="@+id/edit2" android:layout_width="match_parent" android:layout_height="wrap_content"android:hint="請輸入密碼" android:textSize="20sp" android:width="235sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20sp"android:layout_marginLeft="50sp"android:orientation="horizontal"><Button android:onClick="click1" android:layout_width="wrap_content" android:layout_height="50dp"android:text="登錄" android:textSize="20sp" /><!--onClick 就是給該Button組件添加單擊事件處理函數,其值就是寫在MainActivity中的函數名--><Buttonandroid:onClick="click2"android:layout_width="wrap_content"android:layout_height="50dp"android:text="取消"android:textSize="20sp"android:layout_marginLeft="100sp"/></LinearLayout><TextViewandroid:id="@+id/text"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_marginTop="30sp"android:layout_marginLeft="50sp"android:hint="顯示登錄信息"/> </LinearLayout>MainActivity.java
package com.example.userlogin;import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle;public class MainActivity extends AppCompatActivity {EditText et1;EditText et2;TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//通過id獲取對應組件et1 = (EditText) this.findViewById(R.id.edit1);et2 = (EditText) this.findViewById(R.id.edit2);tv = (TextView) this.findViewById(R.id.text);}//登錄按鈕的單擊事件處理方法public void click1(View view){//獲取文本編輯框的內容//trim()去除前后的空格String s1 = et1.getText().toString().trim();String s2 = et2.getText().toString().trim();tv.setText("輸入的用戶名為"+s1+",輸入的密碼是"+s2);}//取消按鈕的單擊事件處理方法public void click2(View view){et1.setText("");et2.setText("");tv.setText("");//MainActivity.this表示MainActivity這個上下文,指示Toast的顯示位置Toast.makeText(MainActivity.this,"取消登錄",Toast.LENGTH_SHORT).show();} }總結
好吧,我屬實是廢話了,不懂的不記得的百度一下,你全都擁有,不用太過糾結。這就是我,一個不靠譜的程序媛。
總結
以上是生活随笔為你收集整理的通过实例学Android应用开发01的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python数据挖掘你准备好了吗?
- 下一篇: 偏振图像传感器