Android用户界面设计学习之旅-第三站
生活随笔
收集整理的這篇文章主要介紹了
Android用户界面设计学习之旅-第三站
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
通過上一篇文章,知道了如何創(chuàng)建一個應(yīng)用程序來顯示一個靜態(tài)的View。然而,在大多數(shù)情況下僅僅顯示是不夠的,應(yīng)該還需要與用戶的交互行為。現(xiàn)在完成一個簡單的和用戶交互的示例。 這個小程序的需求是: 畫面上有三行元素: 第一行是一個TextView,用來顯示文字 第二行是一個EditText,用戶將在此輸入文字 第三行是一個Button 用戶點擊Button之后,會在第一行的TextView中顯示:Hello,(用戶在第二行中輸入的文字)。
比如說用戶在輸入框中輸入:“Roiding.com”,那么點擊Button之后,會在顯示區(qū)域顯示:“Hello, Roiding.com”。 要完成這個程序,大概需要用5步: 第1步,先完成UI的設(shè)計,修改上次項目在res/layout/目錄下main.xml的文件:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
????????android:orientation="vertical"
????????android:layout_width="fill_parent"
????????android:layout_height="fill_parent"??
????????>
??
<TextView
??android:id="@+id/textview_display"
??android:layout_width="fill_parent"
??android:layout_height="wrap_content"
??android:textSize="20px"
??android:textStyle="bold"
??/>
<EditText
??android:id="@+id/edittext_content"
??android:layout_width="fill_parent"
??android:layout_height="wrap_content"
??/>
??
<Button
??android:id="@+id/button_hi"
??android:layout_width="wrap_content"
??android:layout_height="wrap_content"
??android:text="@string/sayhi"
??/>
??
</LinearLayout>如果已經(jīng)過前兩站的學(xué)習(xí)的話,這個文件還是比較容易懂的,不需要太多的解釋了,只解釋一下這幾個語句:
<resources>
????????<string name="hello">Hello, welcome to get aboard Android adventure!</string>
????????<string name="app_name">Adventure</string>
????????<string name="sayhi">Hi</string>
</resources>
第3步,基于上一站UI設(shè)計的基礎(chǔ)上修改Adventure.java,為UI添加交互動作,添加完畢之后:package com.penguin.adventure;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Adventure extends Activity {
??/** Called when the activity is first created. */
??@Override
??public void onCreate(Bundle savedInstanceState) {
????super.onCreate(savedInstanceState);
????setContentView(R.layout.main);
????Button btn = (Button) findViewById(R.id.button_hi);
????btn.setOnClickListener(new View.OnClickListener() {
??????public void onClick(View v) {
????????// TODO Auto-generated method stub
????????EditText edt = (EditText) findViewById(R.id.edittext_content);
????????TextView txv = (TextView) findViewById(R.id.textview_display);
????????String hi = getResources().getString(R.string.sayhi);
????????txv.setText(hi + ", " + edt.getText());
??????}
????});
??}
}這里面有些語句需要解釋:
參考自:http://www.roiding.com/index.php/archives/25
比如說用戶在輸入框中輸入:“Roiding.com”,那么點擊Button之后,會在顯示區(qū)域顯示:“Hello, Roiding.com”。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
????????android:orientation="vertical"
????????android:layout_width="fill_parent"
????????android:layout_height="fill_parent"??
????????>
??
<TextView
??android:id="@+id/textview_display"
??android:layout_width="fill_parent"
??android:layout_height="wrap_content"
??android:textSize="20px"
??android:textStyle="bold"
??/>
<EditText
??android:id="@+id/edittext_content"
??android:layout_width="fill_parent"
??android:layout_height="wrap_content"
??/>
??
<Button
??android:id="@+id/button_hi"
??android:layout_width="wrap_content"
??android:layout_height="wrap_content"
??android:text="@string/sayhi"
??/>
??
</LinearLayout>如果已經(jīng)過前兩站的學(xué)習(xí)的話,這個文件還是比較容易懂的,不需要太多的解釋了,只解釋一下這幾個語句:
- android:id=”@+id/textview_display”
這個是用來為當(dāng)前的View聲明一個ID,這樣,在Java程序中就可以通過這個ID來找到這個元素,進(jìn)而能夠操作這個元素。 - android:textSize=”20px”
聲明字體大小為20px,對于每種View,都會有一系列的可以設(shè)置的屬性,具體的需要參考每個View的說明文檔。
<resources>
????????<string name="hello">Hello, welcome to get aboard Android adventure!</string>
????????<string name="app_name">Adventure</string>
????????<string name="sayhi">Hi</string>
</resources>
第3步,基于上一站UI設(shè)計的基礎(chǔ)上修改Adventure.java,為UI添加交互動作,添加完畢之后:package com.penguin.adventure;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Adventure extends Activity {
??/** Called when the activity is first created. */
??@Override
??public void onCreate(Bundle savedInstanceState) {
????super.onCreate(savedInstanceState);
????setContentView(R.layout.main);
????Button btn = (Button) findViewById(R.id.button_hi);
????btn.setOnClickListener(new View.OnClickListener() {
??????public void onClick(View v) {
????????// TODO Auto-generated method stub
????????EditText edt = (EditText) findViewById(R.id.edittext_content);
????????TextView txv = (TextView) findViewById(R.id.textview_display);
????????String hi = getResources().getString(R.string.sayhi);
????????txv.setText(hi + ", " + edt.getText());
??????}
????});
??}
}這里面有些語句需要解釋:
- Button btn = (Button) findViewById(R.id.button_sayhello)
這個語句中的findViewById()s是一個比較簡單、重要、常用并且是一直用的語句,他的功能就是在當(dāng)前的ContentView中按ID來找到 對應(yīng)的View。例如這里,通過R.id.button_sayhello,就能找到第1步提到的Button了。這個用法和javascript中 的:document.getElementById()有異曲同工之妙。
那這里面的R.id.button_sayhello又是怎么出來的呢?前文中 說過,在Build的時候,aapt會自動生成R.java,對資源文件中的 android:id=”@+id/textview_display”,就會在R.java中向?qū)?yīng)的生成一個ID。 - btn.setOnClickListener(new View.OnClickListener())
現(xiàn)在的GUI程序,好像都喜歡事件驅(qū)動的模型,有人這樣描述過GUI系統(tǒng)本質(zhì): 在事件驅(qū)動下動態(tài)地展現(xiàn)數(shù)據(jù)模型的圖形系統(tǒng)。在這個語句中,setOnClickListener就是為btn添加一個事件監(jiān)聽,監(jiān)聽OnClick事 件,也就是說,如果一旦觸發(fā)btn的OnClick事件,就會執(zhí)行后面定義的功能。
參考自:http://www.roiding.com/index.php/archives/25
轉(zhuǎn)載于:https://blog.51cto.com/penguin7/221631
總結(jié)
以上是生活随笔為你收集整理的Android用户界面设计学习之旅-第三站的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WD1600AAJS AAKS 固件
- 下一篇: Pixysoft.Framework.N