一、基本控件介紹
一般新建組件有兩種方式:XML中定義和Java代碼實現,一般XML中定義較爲常用。
1.Button
按鈕,在main.xml中定義如下:
- <span style="font-family:'Microsoft YaHei';"><Button
- <span style="WHITE-SPACE: pre"> </span>android:layout_width="wrap_content" <!--按鈕寬度匹配文本的大小 -->
- android:layout_height="wrap_content" <!--按鈕高度匹配文本大小 -->
- android:text="文本" <!--按鈕的文本 -->
- android:id="@+id/button1" <!--按鈕的id -->
- ></Button></span>
Button的監聽器:onClickListener;
代碼示例:實現點擊按鈕生成隨機數;
ButtonActivity.java
- <span style="font-family:'Microsoft YaHei';">package org.xiazdong;
- import java.util.Random;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ButtonActivity extends Activity implements OnClickListener{ //實現點擊監聽器
- private Button button;
- private TextView tv;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.button1); //根據ID找組件
- tv = (TextView)findViewById(R.id.tv);
- button.setOnClickListener(this); //爲button設置監聽器
- }
- @Override
- public void onClick(View view) {
- String str = new Random().nextInt()+"";
- tv.setText(str);
- Toast.makeText(this, "點擊了按鈕!!", Toast.LENGTH_SHORT).show(); //設置提示信息
- Builder builder = new AlertDialog.Builder(this); //創建對話框
- builder.setTitle("提示信息").setMessage("點擊了按鈕,隨機數爲:"+str).show(); //設置對話框屬性並顯示
- }
- }</span>
- <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=""
- android:id="@+id/tv"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="點擊生成隨機數"
- android:id="@+id/button1"
- ></Button>
- </LinearLayout></span>
2.ImageButton
和Button的區別爲背景可以自定義圖片,在main.xml中定義如下:
- <span style="font-family:'Microsoft YaHei';"><ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/ib1"
- android:background="@drawable/ic_launcher"/> <!--設置按鈕的背景爲drawable文件夾下的ic_launcher圖片 --></span>
- <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/ib1"
- android:background="@drawable/ic_launcher"/>
- </LinearLayout></span>
ImageButtonActivity.java
- <span style="font-family:'Microsoft YaHei';">package org.xiazdong;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.widget.ImageButton;
- public class ImageButtonActivity extends Activity {
- private ImageButton ib1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ib1 = (ImageButton) findViewById(R.id.ib1);
- ib1.setOnTouchListener(new OnTouchListener(){
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if(event.getAction()==MotionEvent.ACTION_DOWN){ //按下按鈕時
- ib1.setBackgroundResource(R.drawable.logo);
- }
- else if(event.getAction()==MotionEvent.ACTION_UP){ //擡起按鈕時
- ib1.setBackgroundResource(R.drawable.ic_launcher);
- }
- return false;
- }
- });
- }
- }</span>
- <span style="font-family:'Microsoft YaHei';"><EditText
- android:id="@+id/name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="輸入用戶名..."
- android:inputType=""
- /></span>
(1)android:inputType="number":輸入類型爲數字;
(2)android:maxLength="2":輸入最長爲2;
- <p><span style="font-family:'Microsoft YaHei';"><strong>main.xml</strong>
- </span></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="用戶名:" />
- <EditText
- android:id="@+id/name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="輸入用戶名..."
- android:inputType=""
- />
- </LinearLayout>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="密碼:" />
- <EditText
- android:id="@+id/password"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="輸入密碼..."
- android:password="true" />
- </LinearLayout>
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="提交" >
- </Button>
- </LinearLayout>
- </span></pre>
- <pre></pre>
- <span style="font-family:'Microsoft YaHei'"></span><pre class="html" name="code"><p><strong><span style="font-family:'Microsoft YaHei';">EditTextActivity.java</span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class EditTextActivity extends Activity {
- private EditText name;
- private EditText password;
- private Button button;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- name = (EditText) findViewById(R.id.name);
- button = (Button) findViewById(R.id.button);
- password = (EditText) findViewById(R.id.password);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String n = name.getText().toString();
- String p = password.getText().toString();
- Builder builder = new AlertDialog.Builder(EditTextActivity.this); // 創建對話框
- builder.setTitle("提示信息").setMessage("用戶名:" + n + "\n密碼:" + p)
- .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- password.setText(""); //清空密碼
- }
- }).show(); // 設置對話框屬性並顯示
- }
- });
- }
- }</span></pre><pre class="java" name="code"><span style="font-family:微軟雅黑;"></span> </pre>
- <pre></pre>
- <span style="font-family:'Microsoft YaHei'"></span>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>
- <span style="font-family:'Microsoft YaHei';"> <CheckBox
- android:id="@+id/shanghai"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="" /></span>
代碼示例:實現上海、北京、天津的複選框
- <p><strong><span style="font-family:'Microsoft YaHei';">main.xml</span></strong></p><p></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="城市:" />
- <CheckBox
- android:id="@+id/shanghai"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="上海" />
- <CheckBox
- android:id="@+id/beijing"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="北京" />
- <CheckBox
- android:id="@+id/tianjing"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="天津" />
- </LinearLayout></span></pre>
- <pre></pre>
- <span style="font-family:'Microsoft YaHei'"></span>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <p><strong><span style="font-family:'Microsoft YaHei';">CheckBoxActivity.java</span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.Toast;
- public class CheckBoxActivity extends Activity implements
- OnCheckedChangeListener {
- private CheckBox cb1, cb2, cb3;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- cb1 = (CheckBox) findViewById(R.id.shanghai);
- cb2 = (CheckBox) findViewById(R.id.beijing);
- cb3 = (CheckBox) findViewById(R.id.tianjing);
- cb1.setOnCheckedChangeListener(this);
- cb2.setOnCheckedChangeListener(this);
- cb3.setOnCheckedChangeListener(this);
- }
- @Override
- public void onCheckedChanged(CompoundButton buttonView, //buttonView表示改變的框,isChecked表示是選中還是取消選中
- boolean isChecked) {
- if(buttonView==cb1||buttonView==cb2||buttonView==cb3){
- if(isChecked){
- Toast.makeText(this, buttonView.getText()+"被選中",Toast.LENGTH_SHORT).show();
- }
- else{
- Toast.makeText(this, buttonView.getText()+"取消選中",Toast.LENGTH_SHORT).show();
- }
- }
- }
- }</span></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <span style="font-family:'Microsoft YaHei';"><RadioGroup>
- <RadioButton
- android:id="@+id/rb1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="RadioButton1" >
- </RadioButton>
- <RadioButton>
- </RadioButton>
- ......
- </RadioGroup></span>
代碼示例:實現“男、女”單選框;
- <p><strong><span style="font-family:'Microsoft YaHei';">
- main.xml</span></strong></p><p></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <RadioGroup
- android:id="@+id/rg1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <RadioButton
- android:id="@+id/rb1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="男" >
- </RadioButton>
- <RadioButton
- android:id="@+id/rb2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="女" >
- </RadioButton>
- </RadioGroup>
- </LinearLayout></span></pre>
- <pre></pre>
- <span style="font-family:'Microsoft YaHei'"></span>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <p><strong><span style="font-family:'Microsoft YaHei';">RadioButtonActivity.java
- </span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{
- /** Called when the activity is first created. */
- private RadioButton rb1,rb2;
- private RadioGroup rg;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- rb1 = (RadioButton)findViewById(R.id.rb1);
- rb2 = (RadioButton)findViewById(R.id.rb2);
- rg = (RadioGroup)findViewById(R.id.rg1);
- rg.setOnCheckedChangeListener(this);
- }
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- if(group==rg){
- if(rb1.getId()==checkedId){
- Toast.makeText(this, rb1.getText(), Toast.LENGTH_SHORT).show();
- }
- if(rb2.getId()==checkedId){
- Toast.makeText(this, rb2.getText(), Toast.LENGTH_SHORT).show();
- }
- }
- }
- }</span></pre>
- <pre></pre>
- <span style="font-family:'Microsoft YaHei'"></span>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
6.ProgressBar
進度條,在main.xml中定義如下:
- <span style="font-family:'Microsoft YaHei';"><ProgressBar
- android:id="@+id/pb1"
- style="?android:attr/progressBarStyleXxx" <!--設置進度條的樣式,有大、中、小、條狀 -->
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" /></span>
2. 默認,即不設置 圓形中等進度條,動態
3. ?android:attr/progressBarStyleLarge 圓形大進度條,動態
4. ?android:attr/progressBarStyleHorizontal 條狀進度條,靜態
- <p><strong><span style="font-family:'Microsoft YaHei';">
- main.xml</span></strong></p><p></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <ProgressBar
- android:id="@+id/pb4"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="0"
- android:secondaryProgress="0" />
- </LinearLayout></span></pre>
- <pre></pre>
- <span style="font-family:'Microsoft YaHei'"></span><pre class="html" name="code"><p><strong><span style="font-family:'Microsoft YaHei';">ProgressBarActivity.java</span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.widget.ProgressBar;
- import android.widget.Toast;
- public class ProgressBarActivity extends Activity implements Runnable {
- private ProgressBar bar;
- private boolean isFinished;
- Thread t;
- Handler handler = new Handler();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- bar = (ProgressBar) findViewById(R.id.pb4);
- t = new Thread(this);
- t.start();
- }
- public void showToast() {
- handler.post(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(getApplicationContext(), "安裝完成!", //此處需要使用Handler,因爲不能在子線程中使用Toast
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void run() {
- int current = bar.getProgress();
- int currentMax = bar.getMax();
- int secCurrent = bar.getSecondaryProgress();
- while (true) {
- bar.setProgress(current++);
- bar.setSecondaryProgress(secCurrent++);
- if (secCurrent >= currentMax) {
- break;
- }
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- isFinished = true;
- showToast();
- }
- }</span></pre>
- <pre></pre>
- <h2><a name="t16"></a><span style="font-family:'Microsoft YaHei'">7.TextView</span></h2>
- <span style="font-family:'Microsoft YaHei'">文本顯示組件,在main.xml中定義如下:</span>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>
- <span style="font-family:'Microsoft YaHei';"> <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello" /> <!--文本文字 --></span>
(1)簡單的Dialog:
- Builder builder = new Builder(DialogActivity.this); //創建對話框
- builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("對話框標題"); //設置對話框圖標和標題
- builder.setMessage("對話框內容"); //設置對話框信息
- builder.setPositiveButton("Yes", new OnClickListener(){ //設置正確按鈕
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- }
- });
- builder.setNegativeButton("No", new OnClickListener(){ //設置否定按鈕
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- }
- });
- builder.show(); //顯示對話框
實例:添加“上海、北京、天津”的多選框
- <pre class="java" name="code">package org.xiazdong;
- import android.app.Activity;
- import android.app.AlertDialog.Builder;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnMultiChoiceClickListener;
- import android.os.Bundle;
- public class DialogActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Builder builder = new Builder(DialogActivity.this);
- builder.setMultiChoiceItems(new String[] { "上海", "北京", "天津" }, //每項內容
- new boolean[] { true, false, true }, //每項是否沒選中
- new OnMultiChoiceClickListener() { //監聽器
- @Override
- public void onClick(DialogInterface dialog, int which,
- boolean isChecked) {
- }
- }).show();
- }
- }</pre>
- <pre></pre>
- <div><span style="font-family:'Microsoft YaHei'"></span></div>
- <div><span style="font-family:'Microsoft YaHei'"><strong>(3)在dialog中添加列表</strong></span></div>
- <div><span style="font-family:'Microsoft YaHei'"><strong></strong></span></div>
- <div><span style="font-family:'Microsoft YaHei'">builder.setItems(new String[]{"項1","項2"},new OnClickListener(){}); </span></div>
- <div><span style="font-family:'Microsoft YaHei'"></span></div>
- <div><span style="font-family:'Microsoft YaHei'"><strong>(4)在dialog中添加視圖(在main.xml中定義):</strong></span></div>
- <div><span style="font-family:'Microsoft YaHei'"></span></div>
- setView函數實現;<pre class="java" name="code"> Builder builder = new Builder(DialogActivity.this);
- View layout = LayoutInflater.from(this).inflate(R.layout.main, null);
- builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("對話框標題");
- builder.setMessage("對話框內容");
- builder.setPositiveButton("Yes", new OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- }
- });
- builder.setNegativeButton("Yes", new OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- }
- });
- builder.setView(layout);
- builder.show();
- </pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
注:
(1)Activity需要繼承TabActivity 而不是Activity;
(2)OnTabChangedListener爲TabHost的監聽器,存在方法:public void onTagChanged(String tabId);
代碼示例:設置三頁,每頁有各自的內容
- <p><strong>main.xml</strong></p><p></p><pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/main"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/l1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第1頁"></TextView>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/l2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第2頁"></TextView>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/l3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="第3頁"></TextView>
- </LinearLayout>
- </LinearLayout></pre>
- <pre></pre>
- <pre class="html" name="code"><p><strong>TabHostActivity.java</strong></p>
- <pre class="java" name="code">package org.xiazdong;
- import android.app.TabActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- import android.widget.TabHost.OnTabChangeListener;
- import android.widget.TabHost.TabSpec;
- import android.widget.Toast;
- public class TabHostActivity extends TabActivity implements OnTabChangeListener { //繼承TabActivity而不是Activity
- TabHost host;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- host = this.getTabHost(); //新建TabHost
- LayoutInflater.from(this).inflate(R.layout.main, //將main佈局文件映射成tabHost的view
- host.getTabContentView());
- TabSpec t1 = host.newTabSpec("t1"); //新建一個頁,id爲t1
- t1.setIndicator("標籤1"); //設置顯示頁名
- t1.setContent(R.id.l1); //設置頁的內容爲l1佈局,此處可以是佈局或組件
- host.addTab(t1); //加入TabHost中
- TabSpec t2 = host.newTabSpec("t2");
- t2.setIndicator("標籤2",getResources().getDrawable(R.drawable.ic_launcher));
- t2.setContent(R.id.l2);
- host.addTab(t2);
- TabSpec t3 = host.newTabSpec("t3");
- t3.setIndicator("標籤3");
- t3.setContent(R.id.l3);
- host.addTab(t3);
- host.setOnTabChangedListener(this); //設置監聽器
- }
- @Override
- public void onTabChanged(String tabId) {
- Log.v("a","aaaa");
- if(tabId.equals("t1")){
- Toast.makeText(this, "標籤1ing", Toast.LENGTH_LONG).show();
- }
- if(tabId.equals("t2")){
- Toast.makeText(this, "標籤2ing", Toast.LENGTH_LONG).show();
- }
- if(tabId.equals("t3")){
- Toast.makeText(this, "標籤3ing", Toast.LENGTH_LONG).show();
- }
- else{
- Toast.makeText(this, tabId, Toast.LENGTH_LONG).show();
- }
- }
- }</pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>
10.SeekBar
拖動條,在main.xml中定義如下:
- <SeekBar
- android:id="@+id/sb"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
注:存在OnSeekBarChangeListener監聽器,用來監聽SeekBar組件的事件,實現方法:
(1)public void onStartTrackingTouch(SeekBar seekBar); //開始移動時調用
(2)public void onStopTrackingTouch(SeekBar seekbar); //結束移動時調用
(3)public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser); //改變時調用,progress爲當前值
代碼示例:移動SeekBar組件,並在TextView中顯示當前值
- <p><strong>main.xml</strong></p><p></p><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:id="@+id/tv"
- />
- <SeekBar
- android:id="@+id/sb"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout></pre>
- <pre></pre>
- <pre class="html" name="code"><p><strong>SeekBarActivity.java</strong></p><p></p><pre class="java" name="code">package org.xiazdong;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.TextView;
- public class SeekBarActivity extends Activity {
- private TextView tv;
- private SeekBar sb;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv = (TextView) findViewById(R.id.tv);
- sb = (SeekBar) findViewById(R.id.sb);
- sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- tv.setText(progress+"");
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- });
- }
- }</pre>
- <pre></pre>
- <div><span style="font-family:'Microsoft YaHei'"></span></div>
- <div><span style="font-family:'Microsoft YaHei'"></span></div>
- <h2><a name="t22"></a><span style="font-family:'Microsoft YaHei'">11.ListView</span></h2>
- <div><span style="font-family:'Microsoft YaHei'">列表視圖;</span></div>
- <h3><a name="t23"></a><span style="font-family:'Microsoft YaHei'">(1)使用ArrayAdapter實現普通列表</span></h3>
- <div><span style="font-family:'Microsoft YaHei'">ArrayAdapter是一個媒介,通過它可以把數組映射到ListView視圖上。</span></div>
- <div>(1)new ArrayAapter<String>(this,android.R.layout.simple_list_item_1,list); 將list存放到ArrayAdapter中;</div>
- <div>(2)lv.setAdapter(adapter); 爲listView設置Adapter;</div>
- <div><span style="font-family:'Microsoft YaHei'"></span><pre class="java" name="code">package org.xiazdong;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- import android.widget.AdapterView.OnItemClickListener;
- public class ListViewActivity extends Activity implements OnItemClickListener{
- ArrayList<String> list;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- list = new ArrayList<String>();
- list.add("xiazdong-1");
- list.add("xiazdong-2");
- list.add("xiazdong-3");
- ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
- ListView lv = new ListView(this);
- lv.setAdapter(adapter);
- lv.setOnItemClickListener(this);
- this.setContentView(lv);
- }
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- Toast.makeText(this,list.get(arg2), Toast.LENGTH_SHORT).show();
- }
- }</pre><br>
- <h3><a name="t24"></a><span style="font-family:'Microsoft YaHei'">(2)自定義適配器BaseAdapter</span></h3>
- </div>
- <div><br>
- </div>
- <br>
- <h1><a name="t25"></a><span style="font-family:'Microsoft YaHei'">二、4種佈局介紹</span></h1>
- <div><span style="font-family:'Microsoft YaHei'"><br>
- </span></div>
- <span style="font-family:'Microsoft YaHei'">AbsoluteLayout因爲已被廢除,因此不做介紹;<br>
- 只要存在界面,就會有佈局的存在,就像Swing,雖然一個是桌面應用,一個是手機應用,但是他們都差不多。<br>
- </span>
- <h2 style="font-family:monospace; white-space:pre"><a name="t26"></a><img alt="" src="http://images.cnblogs.com/cnblogs_com/skynet/WindowsLiveWriter/Androidview_11A60/ViewGroup%E7%9A%84%E7%BB%A7%E6%89%BF_thumb.jpg"></h2>
- <br>
- 此處因爲佈局非常簡單,所以就不用代碼來講解了。<br>
- <br>
- <br>
- <h2><a name="t27"></a><span style="font-family:'Microsoft YaHei'">1.LinearLayout</span></h2>
- <span style="font-family:'Microsoft YaHei'"><br>
- <br>
- 默認佈局。組件的排列按照預先定義方向很有序的排列,類似於Swing中的FlowLayout;<br>
- 注意點:</span>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>
2.FrameLayout
每個組件都在左上角,如果多個組件一起出現,則會重疊;
3.RelativeLayout
每個組件定位都是按照與其他組件的上下、左右定位;
(1)定位與組件的上下左右
補充:
1.在Activity中根據id獲得strings.xml和main.xml中的內容
getResources().getString(int id);
getResources().getDrawable(int id);
2.鎖定橫豎屏
因爲在CTRL+F11時 會發生問題,因此可以再AndroidManifest.xml的Activity設置:android:screenOrientation=""
(1)portrait:豎屏;
(2)landscape:橫屏;
3.可視化設置佈局、控件
main.xml 如下所示:
多個Activity之間跳轉
代碼示例:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="第一個界面" />
- <TextView
- android:id="@+id/tv1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="" />
- <EditText
- android:id="@+id/e1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="輸入信息"
- />
- <Button
- android:id="@+id/b1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="發送到第二個界面"
- />
- </LinearLayout>
mylayout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="第二個界面" />
- <TextView
- android:id="@+id/tv2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="" />
- <EditText
- android:id="@+id/e2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="輸入信息"
- />
- <Button
- android:id="@+id/b2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="發送到第一個界面"
- />
- </LinearLayout>
MultiActivityActivity.java
- package org.xiazdong;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MultiActivityActivity extends Activity implements OnClickListener{
- private Button b1;
- private EditText e1;
- private TextView tv1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- b1 = (Button)findViewById(R.id.b1);
- e1 = (EditText)findViewById(R.id.e1);
- tv1 = (TextView)findViewById(R.id.tv1);
- Intent i = this.getIntent();
- if(i.getStringExtra("2")!=null){
- tv1.setText(i.getStringExtra("2"));
- }
- b1.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MultiActivityActivity.this,OtherActivity.class);
- intent.putExtra("1", e1.getText().toString());
- this.startActivity(intent);
- }
- }
- package org.xiazdong;
- import android.app.Activity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class OtherActivity extends Activity implements OnClickListener{
- private TextView view ;
- private Button b2;
- private EditText e2;
- private TextView tv2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- view = new TextView(this);
- setContentView(R.layout.mylayout);
- b2 = (Button)findViewById(R.id.b2);
- e2 = (EditText)findViewById(R.id.e2);
- tv2 = (TextView)findViewById(R.id.tv2);
- Intent i = this.getIntent();
- if(i.getStringExtra("1")!=null){
- tv2.setText(i.getStringExtra("1"));
- }
- b2.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(OtherActivity.this,MultiActivityActivity.class);
- intent.putExtra("2", e2.getText().toString());
- this.startActivity(intent);
- }
- }
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
-
<pre></pre>