基于安卓平臺的程序設計:Android用戶界面3_第1頁
基于安卓平臺的程序設計:Android用戶界面3_第2頁
基于安卓平臺的程序設計:Android用戶界面3_第3頁
基于安卓平臺的程序設計:Android用戶界面3_第4頁
基于安卓平臺的程序設計:Android用戶界面3_第5頁
已閱讀5頁,還剩40頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Android用戶界面(3)6.4 相對布局(P56)空間的位置描述為相對于父容器位置,或者相對于其他控件的位置。比如可以先指定一個控件 A 位于父容器中心,然后其他控件的位置可以指定為在 A 下方,和A 左對齊,或在A 右邊,和A上對齊,等用作參照物的控件要先寫相對布局(RelativeLayout)是一種非常靈活的布局方式,能夠通過指定界面元素與其他元素的相對位置關系,確定界面中所有元素的布局位置特點:能夠最大程度保證在各種屏幕類型的手機上正確顯示界面布局6.4 相對布局控件常用屬性(xml中使用):layout_centerHorizontal = “true” 在容器中水平居中layo

2、ut_centerVertical = “true” 在容器中垂直居中layout_alignParentLeft = “true”在容器中靠左邊layout_alignParentTop = “true”在容器中靠上邊.layout_toRightOf = “”id/view01“ 在 view01 的右邊layout_toLeftOf = “”id/view01“ 在 view01 的左邊android:layout_below=“id/view01”在 view01 的下邊android:layout_alignLeft=“id/view01” 和 view01 左對齊android:l

3、ayout_above=“id/view01”在 view01 的上邊.6.4 相對布局右圖比課本上程序多加了:padding 是設置控件內部文字到邊緣的距離,四個方向都一樣。設置單個方向的,可以用 paddingLeft, paddingTop等。在java中用 setPadding或setPaddingLeft 等6.5 絕對布局內部的空間都具體指明了絕對位置(相對于布局左上角的坐標),程序適應性差7 界面控件Android系統的界面控件分為定制控件和系統控件定制控件是用戶獨立開發的控件,或通過繼承并修改系統控件后所產生的新控件。能夠為用戶提供特殊的功能或與眾不同的顯示需求方式系統控件是A

4、ndroid系統提供給用戶已經封裝的界面控件。提供在應用程序開發過程中常見功能控件。系統控件更有利于幫助用戶進行快速開發,同時能夠使Android系統中應用程序的界面保持一致性常見的系統控件包括TextView、EditText、Button、ImageButton、Checkbox、RadioButton、Spinner、ListView和TabHost7.1 TextView和EditTextTextView是靜態文本,也可以通過指定其 background 屬性來指定背景圖,或背景邊框:android:background=drawable/icon“可以指定其 drawableLeft

5、或 drawableRight,drawableTop,drawBottom 等屬性來在左右或上下顯示圖片。android:drawableLeft=drawable/icon”例子見 P64可以指定其 autoLink屬性,使之成為一個鏈接(P62):android:autoLink = “email”android:autoLink = “web”7.2 TextView和EditTextEditText 是從TextView 派生而來的,用于輸入文字。所以TextView能做的,EditText也能做,包括在左邊顯示圖片等。常用屬性(P61):android:selectAllOnFoc

6、us=“true” 獲得輸入焦點時,自動選中全部內容android:hint = “請輸入姓名” 在空的編輯框中顯示:“請輸入姓名”android:password = “true” 密碼方式顯示輸入的文字 android:phoneNumber = “true” 獲得輸入焦點時,自動彈出數字鍵盤7.2 TextView和EditTextandroid:text = “abcd” 開始時顯示的文字程序中獲取EditText中輸入的文字的方法:EditText ed = (EditText)findViewById(R.id.xxx);String s = ed.getText().toStri

7、ng();程序中設置EditText中的文字的方法:ed.setText(“hello”);監聽EditText中的輸入 final txtView = (TextView)findViewById(R.id.xxx); editText = (EditText)findViewById(R.id.YYY); editText.addTextChangedListener(new TextWatcher() Override public void afterTextChanged(Editable s) Override public void beforeTextChanged(CharS

8、equence s, int start, int count, int after) Override public void onTextChanged(CharSequence s, int start, int before, int count) txtView.setText(s.toString(); /txtView內容總是保持和editText一致 );7.3 Button和ImageButtonImageButton上不能顯示文字,但可以同時指定底圖和底色src = “drawable/icon” 指定底圖background = “#ffffff” 指定底色Button上

9、可以同時顯示文字和圖片,但是顯示圖片時,不能指定底色background = “drawable/icon” 指定底圖text = “xxxx” 指定文字7.3 Button和ImageButton要使得按鈕按下和彈起時圖片不一樣,需要使用背景選擇器 selector (P66)參考閱讀:/s/blog_4b93170a0100qhwa.htmldrawable 文件夾下 button_selector.xml (選擇器器)內容:在Button中使用selector: android_background = “drawable/button_selector”在ImageButton中使用s

10、elector: android_src = “drawable/button_selector”在程序中指定按鈕的文字和selector:btNormal.setText(“fdfasdfasdf”); /假設 btNormal是ButtonDrawable dr = getResources().getDrawable(R.drawable.button_selector);btNormal.setBackground(dr );ImageButton指定圖片:imageButton.setImageResource(R.drawable.XXX);更多的selector的設置: 9Pat

11、ch圖片作為按鈕背景(P68)SDK 4.2 可以直接運行 draw9patch.exe,不用管 SwingWorker類參考閱讀:/post/129.html7.4 RadioButton和CheckBoxCheckBox是一個同時可以選擇多個選項的控件RadioButton則是僅可以選擇一個選項的控件RadioGroup是RadioButton的承載體,程序運行時不可見,應用程序中可能包含一個或多個RadioGroup一個RadioGroup包含多個RadioButton,在每個RadioGroup中,用戶僅能夠選擇其中一個RadioButton在xml文檔中,將RadioButton放在

12、RadioGroup中,可以有多個RadioGroup,互不相關。CheckBox和RadioButton都是從 Button派生而來的,所以Button的所有屬性和方法都適用于它們。它們都可以有一個 android:checked屬性取值“true” 或“false” ,用來指明初始狀態是否是選中的7.4 RadioButton和CheckBox在程序中獲取是否選中的狀態:radioButton.isChecked(); 返回值為 true或 falsecheckBox.isChecked(); 返回值為 true或 false在程序中改變選擇狀態:radioButton.setCheck(

13、true); /使其選中,false則使其不選中checkBox.setCheck(true); /使其選中,false則使其不選中RadioButton響應選擇動作 RadioGroup rg = (RadioGroup)findViewById(R.id.XXX); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() public void onCheckedChanged(RadioGroup group, int checkedId) /checkedId 即為被選中的那個 RadioButton

14、的 idif( checkedId = R.id.YYY ) /如果 R.id.YYY這個RadioButton被選中 );CheckBox 響應選擇動作 CheckBox ck = (CheckBox )findViewById(R.id.XXX); ck.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) / isChecked代表被點擊的CheckBox是否被選中

15、 /獲取被點擊的 CheckBox上的文字: String s = arg0.getText().toString(); );7.5 用同一個對象監聽多個事件源class MyListener implements CheckBox.OnCheckedChangeListener public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) switch(buttonView.getId() case R.id.ckBurger:break;case R.id.ckCoke:Log.i(ee,buttonV

16、iew.getText().toString();break;case R.id.ckChips:break; /Log.i 可以在 LogCat的 info里輸出調試信息7.5 用同一個對象監聽多個事件源int CheckBoxIds = R.id.ckBurger, R.id.ckCoke, R.id.ckChips ;CheckBox ckBoxs = new CheckBox3;MyListener listener = new MyListener();for(int k = 0; k 3; +k) ckBoxsk = (CheckBox)findViewById(CheckBoxI

17、dsk); ckBoxsk.setOnCheckedChangeListener(listener);7.6 用Activity作為監聽器監聽多個對象要訣:要讓Activity 實現監聽接口,在事件響應函數里面,用事件源的getId 方法獲得事件源的id,就能知道事件發生在哪里。public class Homework1Activity extends Activity implements Button.OnClickListener, RadioGroup.OnCheckedChangeListener, CheckBox.OnCheckedChangeListener public v

18、oid onCreate(Bundle savedInstanceState) . button1.setOnClickListener(this); button2.setOnClickListener(this); radioGroup1.setOnCheckedChangeListener(this); checkBox1.setOnCheckedChangeListener(this); checkBox2.setOnCheckedChangeListener(this); public void onClick(View arg0) /arg0是事件源switch(arg0.getI

19、d() case R.id.button1:break;case R.id.button2;break; public void onCheckedChanged(CompoundButton arg0, boolean arg1) switch(arg0.getId() case R.id.checkBox1:break;case R.id.checkBox2;break; 7.7 啟動新Activity 并取得其運行結果在一個Activity中啟動另一個Activity,不需要返回結果:Intent intent = new Intent();/* 指定intent要啟動的類 */inte

20、nt.setClass(MainActivity.this, Activity1.class);startActivity(intent);上面代碼出現在MainActivity的某個方法中,則能啟動Activity1。MainActivity 也不會結束,而是被Activity1覆蓋,Activity1運行結束則MainActivity重現。如果希望Activity1啟動了MainActivity就結束,則加一條 :finish(); /結束當前Activity7.7 啟動新Activity 并取得其運行結果2) 在一個Activity中啟動另一個Activity,并要取得新Activity

21、運行的結果(如用戶輸入的字符串):Intent intent = new Intent();/* 指定intent要啟動的類 */intent.setClass(MainActivity.this, Activity1.class);startActivityForResult(intent, XXXX);XXXX是個int 的變量或常量,用來代表所啟動的Activity,相當于新Activity 的某種id要取得返回結果,則 MainActivity不能 finish()7.7 啟動新Activity 并取得其運行結果然后,在 MainActivity中需要寫 :protected void

22、 onActivityResult(int requestCode, int resultCode, Intent data)/新啟動的Activity返回時,此方法被調用 super.onActivityResult(requestCode, resultCode, data); /如果可能啟動多個Activity,可以用requestCode來判斷是從哪個Activity返回的 if (requestCode = XXXX ) else if( requestCode = YYYY) 7.7 啟動新Activity 并取得其運行結果新Activity結束時向啟動它的Activity通過in

23、tent傳遞數據:Intent intent = new Intent();intent.putExtra(“dataId1”,”aaaaaa”); /”aaaaaa”是要傳的數據,此項數據的id是 “dataId1”intent.putExtra(“dataid2,123456);/123456是要傳的數據,此項數據的id是”dataid2”Intent.putExtra( .); /還可以傳更多數據Intent.putExtra();setResult(9999, intent); /9999是返回結果,可以用于傳遞一個整數finish();7.7 啟動新Activity 并取得其運行結果

24、老Activity獲取新Activity傳回的數據:protected void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data); if (requestCode = XXXX ) if( data != null ) String s = data.getStringExtra(“dataId1);int n = data.getIntExtra(“dataId2,-1);/s = “aaaaaa”, n =

25、 123456, resultCode = 9999 else /無返回值,比如在 Activity1中用戶按了硬返回鍵 7.7 啟動新Activity 并取得其運行結果Activity 一定要在 AndroidManifest.xml中聲明,才能起作用,否則導致程序崩潰。 portrait: 規定界面必須是豎的landscape: 規定界面必須是橫著的不指定 screenOrientation 則界面會自動調整橫豎。7.8 狀態開關按鈕ToogleButton(P71)有開和關兩種狀態,可以分別對應于不同的文字關:開:7.8 狀態開關按鈕ToogleButton(P71)toggle.set

26、OnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener() /響應點擊事件Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1)if(arg1) /狀態是開/設置LinearLayout垂直布局test.setOrientation(1);else/設置LinearLayout水平布局test.setOrientation(0); );7.8 狀態開關按鈕ToogleButton(P71)程序中判斷ToogleButton是否是開的:

27、 toggleButton.isChecked()7.9 圖像視圖 ImageView (P75)android:scaleType 屬性 規定圖片如何縮放或移動以適應 ImageView的大小 android:scaleType = “fitCenter” imgView.setScaleType(ImageView.ScaleType.FIT_CENTER) setImageBitmap 方法設置圖片7.10 類似下拉列表框的Spinner控件其內容有兩種生成方式1) 靜態,在xml文件中指定 (P81)2) 在程序中動態指定7.10.1 用ArrayAdapter在程序中動態指定 spi

28、nner內容List list = new ArrayList();Spinner sp = (Spinner) findViewById(R.id.Spinner1);list.(“string1”); list.add(“string2”); list.add(“string3”);ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list); /設置下拉列表的風格adapter.setDropDownViewResource(android.R.layout.simple

29、_spinner_item);/ android.R.layout.simple_spinner_dropdown_item 則選項上有/圓點sp.setAdapter(adapter);/list中的項目成為sp中的選項7.10.2 Spinner響應選擇事件spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() Overridepublic void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) /An Adap

30、terView is a view whose children are determined by an Adapter. Spinner 是AdapterView模版類的派生類 / arg0是事件源 arg2 是被選中項的位置(從0開始), arg3 是其idtxtResult.setText(TextView)arg1).getText() + , + String.valueOf(arg2) + , + String.valueOf(arg3);Overridepublic void onNothingSelected(AdapterView arg0) );7.10.3 修改Spin

31、ner的內容通過修改 Spinner的數據源,來修改Spinner中顯示的選項List list = new ArrayList();Spinner sp = (Spinner) findViewById(R.id.Spinner1);ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);sp.setAdapter(adapter);/list中的項目成為sp中的選項adapter.addItem(“abc”); /將選項“abc”添加進spinnerint c = sp.getCount(); /取選項數目for( int i = 0;i c; + i ) /在每個項目的文字末尾加 “_0”list.set(i, list.get(i) + _0); adapter.notifyDataSetChanged(); /通知sp,數據已改,應刷新m7.10.4 其他Spinner方法 String s = (String)sp.getIt

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論