這一節(jié)將學(xué)習(xí)LinearLayout、EditText旗吁、string資源、Button停局、weight屬性的基本使用很钓,以及使用intent啟動(dòng)Activity并傳遞值香府、在xml文件中定義oncliick事件并實(shí)現(xiàn)、不在xml而使用代碼定義控件码倦。
創(chuàng)建線性布局
建立新的project企孩,建立完成后打開(kāi)activity_main.xml。此 XML 文件定義 MainActivity 的布局袁稽。它包含默認(rèn)的“Hello World”文本視圖勿璃,刪除所有內(nèi)容并插入以下 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns: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="horizontal">
</LinearLayout>
LinearLayout 是一個(gè)視圖組(ViewGroup 的子類(lèi)),它會(huì)按照 android:orientation 屬性的指定推汽,將子視圖設(shè)置為垂直或水平方向布局补疑。LinearLayout 的每個(gè)子視圖都會(huì)按照它們各自在 XML 中的出現(xiàn)順序顯示在屏幕上。
其他兩個(gè)屬性 android:layout_width 和 android:layout_height 則是所有視圖的必備屬性民泵,用于指定它們的尺寸癣丧。
LinearLayout 是布局中的根視圖,因此應(yīng)將寬度和高度設(shè)置為 "match_parent"栈妆,從而填滿(mǎn)可供應(yīng)用使用的整個(gè)屏幕區(qū)域胁编。 該值表示視圖應(yīng)擴(kuò)大其寬度或高度,以匹配父視圖的寬度或高度鳞尔。
添加TextView
在 activity_main.xml 文件的 <LinearLayout> 元素內(nèi)嬉橙,添加以下 <EditText> 元素:
<LinearLayout
xmlns: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="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
</LinearLayout>
-
android:id
這會(huì)為視圖賦予唯一的標(biāo)識(shí)符,您可以使用該標(biāo)識(shí)符從應(yīng)用代碼中引用對(duì)象寥假,例如讀取和操作對(duì)象(
從 XML 引用任何資源對(duì)象時(shí)市框,都需要使用 @ 符號(hào)。 請(qǐng)?jiān)谠摲?hào)后依次輸入資源類(lèi)型糕韧、斜杠和資源名稱(chēng) (edit_message)枫振。
只有在第一次定義資源 ID 時(shí),才需要在資源類(lèi)型之前加一個(gè)加號(hào) (+)萤彩。 當(dāng)您編譯應(yīng)用時(shí)粪滤,SDK 工具會(huì)使用 ID 名稱(chēng)在項(xiàng)目的 R.java 文件中新建一個(gè)引用 EditText 元素的資源 ID。一旦以此方式聲明資源 ID雀扶,其他對(duì)該 ID 的引用皆無(wú)需使用加號(hào)杖小。 只有在指定新資源 ID 時(shí)才必須使用加號(hào),對(duì)于字符串或布局等具體資源則不必如此愚墓。
-
android:layout_width 和 android:layout_height
"wrap_content" 值并不規(guī)定寬度和高度的具體大小予权,而是指定根據(jù)需要縮放視圖,使其適合視圖的內(nèi)容浪册。 如果您要改用 "match_parent"扫腺,則 EditText 元素將填滿(mǎn)屏幕,因?yàn)樗鼤?huì)匹配父 LinearLayout 的大小议经。
-
android:hint
這是文本字段為空時(shí)顯示的默認(rèn)字符串斧账。"@string/edit_message" 并非使用硬編碼字符串作為其值谴返,而是引用另一個(gè)文件中定義的一個(gè)字符串資源。 由于它引用的是一個(gè)具體資源(而不僅僅是標(biāo)識(shí)符)咧织,因此不需要加號(hào)嗓袱。
添加字符串資源
在strings.xml 中添加兩個(gè)新字符串。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
</resources>
對(duì)于用戶(hù)界面中的文本习绢,務(wù)必將每個(gè)字符串都指定為資源渠抹。 字符串資源允許您在單一位置管理所有 UI 文本,從而簡(jiǎn)化文本的查找和更新闪萄。 此外梧却,將字符串外部化還可讓您為每個(gè)字符串資源提供替代定義,從而將您的應(yīng)用本地化為不同的語(yǔ)言败去。
添加按鈕
返回到 activity_main.xml 文件并在 <EditText> 后添加一個(gè)按鈕放航。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
使輸入框填滿(mǎn)屏幕寬度
修改 <EditText>,使用 android:layout_weight 屬性來(lái)指定占比圆裕,使EditText填滿(mǎn)屏幕中除了Button的部分广鳍。
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
完整的 activity_main.xml 布局文件現(xiàn)在看上去應(yīng)該像下面這樣:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
響應(yīng)Send按鈕
將 android:onClick 屬性添加到 <Button> 元素:、
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
每次用戶(hù)點(diǎn)擊按鈕時(shí)吓妆,此屬性均會(huì)提示系統(tǒng)調(diào)用 Activity 中的 sendMessage() 方法赊时。
在代碼中添加 sendMessage() 方法:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
要讓系統(tǒng)將此方法與為 android:onClick 指定的方法名稱(chēng)匹配,簽名必須與所示內(nèi)容完全相同行拢。具體而言祖秒,該方法必須:
- 是公共方法
- 具有空返回值
- 以 View 作為唯一參數(shù)(這將是之前點(diǎn)擊的 View)
接下來(lái),您需要填寫(xiě)此方法以讀取文本字段的內(nèi)容舟奠,并將該文本傳遞給另一個(gè) Activity竭缝。
構(gòu)建一個(gè) Intent
在 MainActivity.java 中,將如下所示代碼添加到 sendMessage():
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Intent 構(gòu)造函數(shù)采用兩個(gè)參數(shù):
- Context 是第一個(gè)參數(shù)(之所以使用 this 沼瘫,是因?yàn)?Activity 類(lèi)是 Context 的子類(lèi))
- 應(yīng)用組件的 Class歌馍,系統(tǒng)應(yīng)將 Intent(在本例中,為應(yīng)啟動(dòng)的 Activity)傳遞至該類(lèi)晕鹊。
putExtra() 方法將 EditText 的值添加到 Intent。Intent 能夠以名為 extra 的鍵值對(duì)形式攜帶數(shù)據(jù)類(lèi)型暴浦。您的鍵是一個(gè)公共常量 EXTRA_MESSAGE溅话,因?yàn)橄乱粋€(gè) Activity 將使用該鍵來(lái)檢索文本值。為 Intent extra 定義鍵時(shí)最好使用應(yīng)用的軟件包名稱(chēng)作為前綴歌焦。這可以確保在您的應(yīng)用與其他應(yīng)用交互過(guò)程中這些鍵始終保持唯一飞几。
startActivity() 方法將啟動(dòng) Intent 指定的 DisplayMessageActivity 實(shí)例。現(xiàn)在独撇,您需要?jiǎng)?chuàng)建類(lèi)屑墨。
創(chuàng)建第二個(gè) Activity
1.在 Project 窗口中躁锁,右鍵點(diǎn)擊 app 文件夾并選擇 New > Activity > Empty Activity。
2.在 Configure Activity 窗口中卵史,為 Activity Name 輸入 “DisplayMessageActivity”战转,然后點(diǎn)擊 Finish。
Android Studio 自動(dòng)執(zhí)行三項(xiàng)操作:
- 使用必需的 onCreate() 方法的實(shí)現(xiàn)創(chuàng)建類(lèi) DisplayMessageActivity.java以躯。
- 創(chuàng)建對(duì)應(yīng)的布局文件 activity_display_message.xml
- 在 AndroidManifest.xml 中添加必需的 <activity> 元素槐秧。
顯示消息
在 DisplayMessageActivity.java 中,向 onCreate() 方法添加下列代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
}
1.調(diào)用 getIntent() 采集啟動(dòng) Activity 的 intent忧设。無(wú)論用戶(hù)如何導(dǎo)航到目的地刁标,每個(gè) Activity 都由一個(gè) Intent 調(diào)用。 調(diào)用 getStringExtra() 將檢索第一個(gè) Activity 中的數(shù)據(jù)址晕。
2.您可以編程方式創(chuàng)建 TextView 并設(shè)置其大小和消息膀懈。
3.您可將 TextView 添加到 R.id.activity_display_message 標(biāo)識(shí)的布局。您可將布局投射到 ViewGroup谨垃,因?yàn)樗撬胁季值某?lèi)且包含 addView() 方法启搂。