2020-10-11

Android是由Google支持的一個開放的免費手機開發平臺,不僅僅是一個操作系統,更是一個完整的軟件框架,基于Linux內核

一、Android應用開發環境

1.JDK? ? 2.Software Development Kit(SDK)? ?3.Android Studio

二、Android程序結構

1.存放程序的代碼和資源等文件(main、test)

2.程序的gradle構建腳本(build.gradle)

3.指定項目中所使用的SDK路徑(local.properties)

4.配置在Android中使用的子項目(settings.gradle)

三、資源管理與使用

圖片資源調用

1.通過Java代碼

//調用mipmap文件夾中的資源文件

getResources().getDrawable(R.mipmap.ic_launcher);

//調用以drawable開頭的文件夾中的資源文件

getResources().getDrawable(R.drawable.icon);

2.在XML布局文件中調用圖片資源

@mipmap/ic_launcher

@drawable/icon

主題資源

標簽:<style></style> 定義主題

<item></item> 設置主題樣式

布局

LinearLayout

RelativeLayout

TableLayout

FrameLayout

GridLyout

View

部分組件

TextView

EditText

Button

CheckBox

RadioBox

Boggle

Switch

ImageView

ImageButton

ZoomButton

事件 (重要)

Android事件處理方式分兩種方式:

基于監聽的事件處理

基于回調的事件處理

View.OnClickListener:單擊事件的事件監聽器必須實現的接口。

View.OnCreateContextMenu Listener :創建上下文菜單事件的事件監聽器必須實現的接口。

View.onFocusChangeListener:焦點改變事件的事件監聽器必須實現的接口。

View.OnKeyListener:按鍵事件的事件監聽器必須實現的接口。

View.OnLongClickListener:長按事件的事件監聽器必須實現的接口。

View.OnTouchListener:觸摸事件的事件監聽器必須實現的接口。

實例:

MainActivity.java

public class MainActivity extends AppCompatActivity {

? ? EditText mNameEt = null;

? ? EditText mPasswordEt = null;

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.layout);

? ? ? ? // 獲取界面組件

? ? ? ? mNameEt = findViewById(R.id.name_et);

? ? ? ? mPasswordEt = findViewById(R.id.pwd_et);

? ? ? ? Button btnClick =? findViewById(R.id.login_btn);

? ? ? ? btnClick.setOnClickListener(new View.OnClickListener(){

? ? ? ? ? ? @Override

? ? ? ? ? ? public void onClick(View view){

? ? ? ? ? ? ? ? String name = mNameEt.getText().toString();

? ? ? ? ? ? ? ? String password = mPasswordEt.getText().toString();

? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "按鈕被點擊" + name + password,

? ? ? ? ? ? ? ? ? ? ? ? Toast.LENGTH_SHORT).show();

? ? ? ? ? ? }

? ? ? ? });

? ? }

layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:orientation="vertical">

? ? <TextView

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="用戶名: "

? ? ? ? android:textSize="16sp" />

? ? <EditText

? ? ? ? android:id="@+id/name_et"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:hint="請輸入用戶名"/>

? ? <TextView

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="密碼: "

? ? ? ? android:textSize="16sp"/>

? ? <EditText

? ? ? ? android:id="@+id/pwd_et"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:hint="請輸入密碼"/>

? ? <Button

? ? ? ? android:id="@+id/login_btn"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="普通按鈕"/>

</LinearLayout>

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。