學習android第二天
活動
活動是一種可以包含用戶界面的組件,主要用于與用戶進行互動,
在創建專案時選擇Add No Activity,即可手動創建活動
先把專案結構從Android模式改為Project模式,
右擊com.example.activities包—>New—>Activity—>Empty Activity,
不勾選Generate Layout File和Launcher Activity,
勾選Generate Layout File 表示會自動為這個活動創建一個對應的布局檔案
勾選Launcher Activity 表示自動將這個活動設定為當前專案的主活動
創建和加載布局
這里先放著后面補充
布局檔案中已有一個LinearLayout元素,接下來嘗試添加一個按鈕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
/>
</LinearLayout>
注意:
- android:id 是給當前元素定義一個唯一識別符號
- 在XML中參考一個id:@id/id_name
- 在XML中定義一個id:@+id/id_name
- android:layout_width和android:layout_height分別指定元素的寬度和高度
- match_parent表示和父元素一樣寬(高),wrap_content表示當前元素的高度剛好包含里面的內容
加載布局
可以在活動中呼叫setContentView()方法來給當前活動加載一個布局,
如:setContentView(R.layout.first_layout);
我們需要傳入一個布局檔案的id
專案中添加的任何資源都會在R檔案中生成一個相應的資源id,我們只需呼叫R.layout.first_layout就能得到first_layout.xml布局的id
注冊活動
所有活動都要在AndroidManifest.xml中進行注冊才能生效
我們可以看到Android Studio自動為我們注冊好了
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ActivityTest">
<activity android:name=".NormalActivity">
</activity>
</application>
</manifest>
- 活動注冊要放在標簽中
- 通過標簽對活動進行注冊
- android:name=".NormalActivity"中填入的活動名是對com.example.activitytest.NormalActivity的縮寫
因為我們之前在標簽中已經通過package屬性指定了程式包名為com.example.activitytest
因此這里可以省略,直接使用.NormalActivity即可
但是啟動程式時,先啟動哪個活動呢?
要配置主活動,就要在標簽的內部加入標簽,
并在這個標簽里添加 和 這兩句宣告
還可以使用android:label指定活動中標題欄的內容,標題欄是顯示在活動最頂部的
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ActivityTest">
<activity android:name=".NormalActivity"
android:label="This is NormalActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以上操作可以使點擊桌面應用程式圖示時首先打開的就是NormalActivity,
如果沒有宣告主活動,程式仍然可以正常安裝,只是無法在啟動器中看到和打開這個程式,
這個程式一般作為第三方服務供其他應用在內部進行呼叫,如支付寶快捷支付,
在活動中使用Toast
Toast是Android系統提供的一種非常好的提醒方式,在程式中可以使用它將一些短小的資訊通知給用戶,這些資訊在一段時間后會自動消失,并不會占用任何的螢屏空間,
界面上有一個按鈕,我們可以設定讓點擊這個按鈕時彈出一個Toast,在onCreat()方法中添加如下代碼:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
Button button1 = (Button)findViewById(R.id.button_1);
button1.setOnClickListener(new View.OnClickListener()){
@Override
public void onClick(View v){
Toast.makeText(NormalActivity.this,"You click button1",
Toast.LENGTH_SHORT).show();
}
});
}
- 活動中,我們可以通過 findViewById() 方法獲取到在布局檔案定義的元素,只需要傳入 R.id.button_1 就能獲取按鈕實體
- findViewById() 方法回傳的是一個View物件,所以我們需要向下轉型將它轉成Button物件
- 獲取按鈕實體后我們使用 setOnClickListener() 方法為按鈕注冊一個監聽器,點擊按鈕時就會執行監聽器中的 onClick() 方法
- 要使用 Toast 只需通過靜態方法makeText() 創建出一個Toast物件,然后呼叫show()將Toast顯示出來即可
- makeText()方法需要傳入三個引數,第一個引數是Context,是Toast要求的背景關系,第二個引數是Toast顯示的文本內容,第三個引數是Toast顯示的時長,有兩個內置常量可選擇 Toast.LENGTH_SHORT 和 Toast.LENGTH_LONG
在活動中使用Menu
后面補充
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/259505.html
標籤:其他
