編輯:中級開發
6、Tab的Demo
tab.XML
vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>
02.<FrameLayout XMLns:android="http://schemas.android.com/apk/res/android"
03. android:layout_width="fill_parent" android:layout_height="fill_parent">
04. <!-- Tab 1 的內容 -->
05. <TextView android:id="@+id/vIEw1" android:layout_width="fill_parent"
06. android:layout_height="fill_parent" android:text="tab1 content" />
07. <!-- Tab 2 的內容 -->
08. <TextView android:id="@+id/vIEw2" android:layout_width="fill_parent"
09. android:layout_height="fill_parent" android:text="tab2 content" />
10.</FrameLayout>
<?XML version="1.0" encoding="utf-8"?>
<FrameLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- Tab 1 的內容 -->
<TextView android:id="@+id/vIEw1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="tab1 content" />
<!-- Tab 2 的內容 -->
<TextView android:id="@+id/vIEw2" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="tab2 content" />
</FrameLayout>
_Tab.Java
vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;
02.import android.app.TabActivity;
03.import android.content.Intent;
04.import android.os.Bundle;
05.import android.vIEw.LayoutInflater;
06.import android.widget.TabHost;
07.//實現 Tab 功能的話要繼承 TabActivity
08.public class _Tab extends TabActivity {
09. @Override
10. protected void onCreate(Bundle savedInstanceState) {
11. //TODO Auto-generated method stub
12. super.onCreate(savedInstanceState);
13. TabHost tabHost=getTabHost();
14. LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentVIEw(), true);
15. //Tab 1 的內容
16. tabHost.addTab(tabHost.newTabSpec("tab1")
17. .setIndicator("tab1")
18. .setContent(R.id.vIEw1));
19. //Tab 2 的內容(設置了 Tab 圖片)
20. tabHost.addTab(tabHost.newTabSpec("tab2")
21. .setIndicator("tab2", getResources().getDrawable(R.drawable.icon01))
22. .setContent(R.id.vIEw2));
23. //Tab 3 的內容(設置 Tab 的內容為指定的 Activity)
24. tabHost.addTab(tabHost.newTabSpec("tab3")
25. .setIndicator("tab3")
26. .setContent(new Intent(this, _TextVIEw.class)));
27. }
28.}
package com.webabcd.vIEw;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.vIEw.LayoutInflater;
import android.widget.TabHost;
//實現 Tab 功能的話要繼承 TabActivity
public class _Tab extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TabHost tabHost=getTabHost();
LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentVIEw(), true);
//Tab 1 的內容
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("tab1")
.setContent(R.id.vIEw1));
//Tab 2 的內容(設置了 Tab 圖片)
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("tab2", getResources().getDrawable(R.drawable.icon01))
.setContent(R.id.vIEw2));
//Tab 3 的內容(設置 Tab 的內容為指定的 Activity)
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("tab3")
.setContent(new Intent(this, _TextVIEw.class)));
}
}
7、Spinner的Demo
spinner.XML
vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
03. android:orIEntation="vertical" android:layout_width="fill_parent"
04. android:layout_height="fill_parent">
05. <TextVIEw android:layout_width="fill_parent"
06. android:layout_height="wrap_content" android:id="@+id/textVIEw" />
07. <!--
08. Spinner - 下拉框控件
09. -->
10. <Spinner android:id="@+id/spinner" android:layout_width="fill_parent"
11. android:layout_height="wrap_content" />
12.</LinearLayout>
<?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:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/textVIEw" />
<!--
Spinner - 下拉框控件
-->
<Spinner android:id="@+id/spinner" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
_Spinner.Java
vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;
02.import android.app.Activity;
03.import android.os.Bundle;
04.import android.view.VIEw;
05.import android.widget.AdapterVIEw;
06.import android.widget.ArrayAdapter;
07.import android.widget.Spinner;
08.import android.widget.TextVIEw;
09.public class _Spinner extends Activity {
10. @Override
11. protected void onCreate(Bundle savedInstanceState) {
12. //TODO Auto-generated method stub
13. super.onCreate(savedInstanceState);
14. this.setContentVIEw(R.layout.spinner);
15. setTitle("Spinner");
16. Spinner spinner=(Spinner) findVIEwById(R.id.spinner);
17. //設置下拉框控件的標題文本
18. spinner.setPrompt("請選擇");
19. //實例化適配器,指定顯示格式及數據源
20. ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(
21. this, R.array.colors, android.R.layout.simple_spinner_item);
22. adapter.setDropDownVIEwResource(android.R.layout.simple_spinner_dropdown_item);
23. spinner.setAdapter(adapter);
24. //setOnItemSelectedListener() - 響應下拉框的選中值發生變化的事件
25. spinner.setOnItemSelectedListener(new AdapterVIEw.OnItemSelectedListener() {
26. @Override
27. public void onItemSelected(AdapterView<?> arg0, VIEw arg1,
28. int arg2, long arg3) {
29. TextView textView=(TextView)_Spinner.this.findViewById(R.id.textVIEw);
30. textView.setText(((TextVIEw)arg1).getText());
31. }
32. @Override
33. public void onNothingSelected(AdapterVIEw<?> arg0) {
34.
35. }
36. });
37. }
38.}
package com.webabcd.vIEw;
import android.app.Activity;
import android.os.Bundle;
import android.view.VIEw;
import android.widget.AdapterVIEw;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextVIEw;
public class _Spinner extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentVIEw(R.layout.spinner);
setTitle("Spinner");
Spinner spinner=(Spinner) findVIEwById(R.id.spinner);
//設置下拉框控件的標題文本
spinner.setPrompt("請選擇");
//實例化適配器,指定顯示格式及數據源
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(
this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownVIEwResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//setOnItemSelectedListener() - 響應下拉框的選中值發生變化的事件
spinner.setOnItemSelectedListener(new AdapterVIEw.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, VIEw arg1,
int arg2, long arg3) {
TextView textView=(TextView)_Spinner.this.findViewById(R.id.textVIEw);
textView.setText(((TextVIEw)arg1).getText());
}
@Override
public void onNothingSelected(AdapterVIEw<?> arg0) {
}
});
}
}
8、Chronometer的Demo
chronometer.XML
vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
03. android:orIEntation="vertical" android:layout_width="fill_parent"
04. android:layout_height="fill_parent">
05. <!--
06. Chronometer - 計時器控件
07. -->
08. <Chronometer android:id="@+id/chronometer"
09. android:layout_width="wrap_content" android:layout_height="wrap_content" />
10. <Button android:id="@+id/btnStart" android:layout_width="wrap_content"
11. android:layout_height="wrap_content" android:text="開始計時">
12. <requestFocus />
13. </Button>
14. <Button android:id="@+id/btnStop" android:layout_width="wrap_content"
15. android:layout_height="wrap_content" android:text="停止計時">
16. </Button>
17. <Button android:id="@+id/btnReset" android:layout_width="wrap_content"
18. android:layout_height="wrap_content" android:text="計時器復位">
19. </Button>
20.</LinearLayout>
<?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">
<!--
Chronometer - 計時器控件
-->
<Chronometer android:id="@+id/chronometer"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/btnStart" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="開始計時">
<requestFocus />
</Button>
<Button android:id="@+id/btnStop" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="停止計時">
</Button>
<Button android:id="@+id/btnReset" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="計時器復位">
</Button>
</LinearLayout>
_Chronometer.Java
vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;
02.import android.app.Activity;
03.import android.os.Bundle;
04.import android.os.SystemClock;
05.import android.view.VIEw;
06.import android.view.VIEw.OnClickListener;
07.import android.widget.Button;
08.import android.widget.Chronometer;
09.public class _Chronometer extends Activity {
10. private Chronometer mChronometer;
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13. //TODO Auto-generated method stub
14. super.onCreate(savedInstanceState);
15. this.setContentVIEw(R.layout.chronometer);
16. setTitle("Chronometer");
17. Button button;
18. mChronometer=(Chronometer) findVIEwById(R.id.chronometer);
19. //設置計時器所顯示的時間格式
20. mChronometer.setFormat("計時:(%s)");
21. button=(Button) findVIEwById(R.id.btnStart);
22. button.setOnClickListener(mStartListener);
23. button=(Button) findVIEwById(R.id.btnStop);
24. button.setOnClickListener(mStopListener);
25. button=(Button) findVIEwById(R.id.btnReset);
26. button.setOnClickListener(mResetListener);
27. }
28. VIEw.OnClickListener mStartListener=new OnClickListener() {
29. public void onClick(VIEw v) {
30. //啟動計時器
31. mChronometer.start();
32. }
33. };
34. VIEw.OnClickListener mStopListener=new OnClickListener() {
35. public void onClick(VIEw v) {
36. //暫停計時器
37. mChronometer.stop();
38. }
39. };
40. VIEw.OnClickListener mResetListener=new OnClickListener() {
41. public void onClick(VIEw v) {
42. //復位計時器,即停止計時器
43. mChronometer.setBase(SystemClock.elapsedRealtime());
44. }
45. };
46.}
package com.webabcd.vIEw;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.VIEw;
import android.view.VIEw.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class _Chronometer extends Activity {
private Chronometer mChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentVIEw(R.layout.chronometer);
setTitle("Chronometer");
Button button;
mChronometer=(Chronometer) findVIEwById(R.id.chronometer);
//設置計時器所顯示的時間格式
mChronometer.setFormat("計時:(%s)");
button=(Button) findVIEwById(R.id.btnStart);
button.setOnClickListener(mStartListener);
button=(Button) findVIEwById(R.id.btnStop);
button.setOnClickListener(mStopListener);
button=(Button) findVIEwById(R.id.btnReset);
button.setOnClickListener(mResetListener);
}
VIEw.OnClickListener mStartListener=new OnClickListener() {
public void onClick(VIEw v) {
//啟動計時器
mChronometer.start();
}
};
VIEw.OnClickListener mStopListener=new OnClickListener() {
public void onClick(VIEw v) {
//暫停計時器
mChronometer.stop();
}
};
VIEw.OnClickListener mResetListener=new OnClickListener() {
public void onClick(VIEw v) {
//復位計時器,即停止計時器
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
}
9、ScrollVIEw的Demo
scrollvIEw.XML
vIEw plaincopy to clipboardprint?
01.<?XML version="1.0" encoding="utf-8"?>
02.<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
03. android:orIEntation="vertical" android:layout_width="fill_parent"
04. android:layout_height="fill_parent">
05. <!--
06. ScrollVIEw - 滾動條控件
07. scrollbarStyle - 滾動條的樣式
08. -->
09. <ScrollVIEw android:id="@+id/scrollVIEw"
10. android:layout_width="fill_parent" android:layout_height="200px"
11. android:scrollbarStyle="outsideOverlay" android:background="@android:drawable/edit_text">
12. <TextVIEw android:layout_width="fill_parent"
13. android:layout_height="wrap_content" android:id="@+id/textVIEw" />
14. </ScrollVIEw>
15.</LinearLayout>
<?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">
<!--
ScrollVIEw - 滾動條控件
scrollbarStyle - 滾動條的樣式
-->
<ScrollVIEw android:id="@+id/scrollVIEw"
android:layout_width="fill_parent" android:layout_height="200px"
android:scrollbarStyle="outsideOverlay" android:background="@android:drawable/edit_text">
<TextVIEw android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/textVIEw" />
</ScrollVIEw>
</LinearLayout>
_ScrollVIEw.Java
vIEw plaincopy to clipboardprint?
01.package com.webabcd.vIEw;
02.import android.app.Activity;
03.import android.os.Bundle;
04.import android.widget.TextVIEw;
05.public class _ScrollVIEw extends Activity {
06. @Override
07. protected void onCreate(Bundle savedInstanceState) {
08. //TODO Auto-generated method stub
09. super.onCreate(savedInstanceState);
10. this.setContentView(R.layout.scrollvIEw);
11. setTitle("ScrollVIEw");
12. TextView textView=(TextView)this.findViewById(R.id.textVIEw);
13. textVIEw.setText("a\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na");
14. }
15.}
簡介: 本文是共兩部分的系列文章 “android 和 iPhone 浏覽器之戰” 的第 2 部分,主要關注為 iPhone 和 Android 開發基於浏覽
本文節選於機械工業出版社推出的《Android應用開發揭秘》一書,作者為楊豐盛。本書內容全面,詳細講解了Android框架、android組件、用戶界面開發、游戲開發、
有關android SDK自帶的性能分析調試工具TraceVIEw使用方法,我們在android.os.Debug調試工具使用方法 簡單的說過,有關實際使用如
Fragment是Android honeycomb 3.0新增的概念,Fragment名為碎片不過卻和Activity十分相似,下面Android123介紹下andr