Android 各种基础控件布局

来源:互联网 发布:java. base64decode 编辑:程序博客网 时间:2024/05/29 02:37



一、基本控件介绍


一般新建组件有两种方式:XML中定义和Java代码实现,一般XML中定义较为常用。

 

1.Button


按钮,在main.xml中定义如下:

 

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><Button   
  2. <span style="WHITE-SPACE: pre"> </span>android:layout_width="wrap_content"  <!--按钮宽度匹配文本的大小 -->  
  3.     android:layout_height="wrap_content"  <!--按钮高度匹配文本大小 -->  
  4.     android:text="文本"   <!--按钮的文本 -->  
  5.     android:id="@+id/button1"  <!--按钮的id -->  
  6. ></Button></span>  

Button的监听器:onClickListener;

需要实现方法:public void onClick(View v); v表示触发的控件,比如按钮

代码示例:实现点击按钮生成随机数;


ButtonActivity.java

[java] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  2. import java.util.Random;  
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12. public class ButtonActivity extends Activity implements OnClickListener{    //实现点击监听器  
  13.     private Button button;  
  14.     private TextView tv;  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         button = (Button)findViewById(R.id.button1);        //根据ID找组件  
  20.         tv = (TextView)findViewById(R.id.tv);  
  21.         button.setOnClickListener(this);        //为button设置监听器  
  22.     }  
  23.     @Override  
  24.     public void onClick(View view) {  
  25.         String str = new Random().nextInt()+"";  
  26.         tv.setText(str);  
  27.         Toast.makeText(this"点击了按钮!!", Toast.LENGTH_SHORT).show(); //设置提示信息  
  28.          Builder builder = new AlertDialog.Builder(this);           //创建对话框  
  29.          builder.setTitle("提示信息").setMessage("点击了按钮,随机数为:"+str).show(); //设置对话框属性并显示  
  30.     }  
  31. }</span>  


main.xml

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text=""   
  11.         android:id="@+id/tv"  
  12.         />  
  13.     <Button   
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="点击生成随机数"  
  17.         android:id="@+id/button1"  
  18.         ></Button>  
  19. </LinearLayout></span>  

2.ImageButton


和Button的区别为背景可以自定义图片,在main.xml中定义如下:

 

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><ImageButton   
  2.     android:layout_width="wrap_content"   
  3.     android:layout_height="wrap_content"  
  4.     android:id="@+id/ib1"  
  5.     android:background="@drawable/ic_launcher"/>  <!--设置按钮的背景为drawable文件夹下的ic_launcher图片 --></span>  


代码示例:实现点击图片按钮就切换图片;


main.xml
[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.     <ImageButton   
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/ib1"  
  15.         android:background="@drawable/ic_launcher"/>  
  16. </LinearLayout></span>  

ImageButtonActivity.java

[java] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnTouchListener;  
  8. import android.widget.ImageButton;  
  9.   
  10. public class ImageButtonActivity extends Activity {  
  11.     private ImageButton ib1;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         ib1 = (ImageButton) findViewById(R.id.ib1);  
  17.         ib1.setOnTouchListener(new OnTouchListener(){  
  18.   
  19.             @Override  
  20.             public boolean onTouch(View v, MotionEvent event) {  
  21.                 if(event.getAction()==MotionEvent.ACTION_DOWN){  //按下按钮时  
  22.                     ib1.setBackgroundResource(R.drawable.logo);  
  23.                 }  
  24.                 else if(event.getAction()==MotionEvent.ACTION_UP){  //抬起按钮时  
  25.                     ib1.setBackgroundResource(R.drawable.ic_launcher);  
  26.                 }  
  27.                 return false;  
  28.             }  
  29.               
  30.         });  
  31.     }  
  32. }</span>  

3.EditText


文本框,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><EditText  
  2.      android:id="@+id/name"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="wrap_content"  
  5.      android:hint="输入用户名..."   
  6.      android:inputType=""  
  7. /></span>  

可以在<EditText>中设置以下属性:
(1)android:inputType="number":输入类型为数字;

(2)android:maxLength="2":输入最长为2;

(3)android:singleLine="true":只能单行显示;

(4)android:password="true" :输入的形式为密码

(5)android:numeric="integer":输入整数

代码示例:实现用户登录;


[html] view plaincopy
  1. <p><span style="font-family:'Microsoft YaHei';"><strong>main.xml</strong>  
  2. </span></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         xmlns:android="http://schemas.android.com/apk/res/android"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="用户名:" />  
  18.   
  19.         <EditText  
  20.             android:id="@+id/name"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:hint="输入用户名..."   
  24.             android:inputType=""  
  25.             />  
  26.     </LinearLayout>  
  27.   
  28.     <LinearLayout  
  29.         xmlns:android="http://schemas.android.com/apk/res/android"  
  30.         android:layout_width="fill_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:orientation="horizontal" >  
  33.  <TextView  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:text="密码:" />  
  37.         <EditText  
  38.             android:id="@+id/password"  
  39.             android:layout_width="fill_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:hint="输入密码..."  
  42.             android:password="true" />  
  43. </LinearLayout>  
  44.         <Button  
  45.             android:id="@+id/button"  
  46.             android:layout_width="fill_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:text="提交" >  
  49.         </Button>  
  50.     </LinearLayout>  
  51. </span></pre>  
  52. <pre></pre>  
  53. <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;  
  54.   
  55. import android.app.Activity;  
  56. import android.app.AlertDialog;  
  57. import android.app.AlertDialog.Builder;  
  58. import android.content.DialogInterface;  
  59. import android.os.Bundle;  
  60. import android.view.View;  
  61. import android.view.View.OnClickListener;  
  62. import android.widget.Button;  
  63. import android.widget.EditText;  
  64.   
  65. public class EditTextActivity extends Activity {  
  66.     private EditText name;  
  67.     private EditText password;  
  68.     private Button button;  
  69.   
  70.     @Override  
  71.     public void onCreate(Bundle savedInstanceState) {  
  72.         super.onCreate(savedInstanceState);  
  73.         setContentView(R.layout.main);  
  74.         name = (EditText) findViewById(R.id.name);  
  75.         button = (Button) findViewById(R.id.button);  
  76.         password = (EditText) findViewById(R.id.password);  
  77.         button.setOnClickListener(new OnClickListener() {  
  78.             @Override  
  79.             public void onClick(View v) {  
  80.                 String n = name.getText().toString();  
  81.                 String p = password.getText().toString();  
  82.                 Builder builder = new AlertDialog.Builder(EditTextActivity.this); // 创建对话框  
  83.                 builder.setTitle("提示信息").setMessage("用户名:" + n + "\n密码:" + p)  
  84.                         .setPositiveButton("知道了", new DialogInterface.OnClickListener() {  
  85.                             @Override  
  86.                             public void onClick(DialogInterface dialog, int which) {  
  87.                                 password.setText("");   //清空密码  
  88.                             }  
  89.                         }).show();                      // 设置对话框属性并显示  
  90.             }  
  91.         });  
  92.     }  
  93. }</span></pre><pre class="java" name="code"><span style="font-family:微软雅黑;"></span> </pre>  
  94. <pre></pre>  
  95. <span style="font-family:'Microsoft YaHei'"></span>  
  96. <pre></pre>  
  97. <pre></pre>  
  98. <pre></pre>  
  99. <pre></pre>  
  100. <pre></pre>  
  101. <pre></pre>  
  102. <pre></pre>  
  103. <pre></pre>  
  104. </pre>  

4.CheckBox


多选框,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"> <CheckBox  
  2.         android:id="@+id/shanghai"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:text="" /></span>  

onCheckedChangeListener监听器是专门对CheckBox进行监听,实现方法:public void onCheckedChanged(CompundButton buttonView,boolean isChecked);

代码示例:实现上海、北京、天津的复选框

[html] view plaincopy
  1. <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"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="城市:" />  
  11.   
  12.     <CheckBox  
  13.         android:id="@+id/shanghai"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="上海" />  
  17.   
  18.     <CheckBox  
  19.         android:id="@+id/beijing"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="北京" />  
  23.   
  24.     <CheckBox  
  25.         android:id="@+id/tianjing"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="天津" />  
  29.   
  30. </LinearLayout></span></pre>  
  31. <pre></pre>  
  32. <span style="font-family:'Microsoft YaHei'"></span>  
  33. <pre></pre>  
  34. <pre></pre>  
  35. <pre></pre>  
  36. <pre></pre>  
[html] view plaincopy
  1. <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;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CheckBox;  
  6. import android.widget.CompoundButton;  
  7. import android.widget.CompoundButton.OnCheckedChangeListener;  
  8. import android.widget.Toast;  
  9.   
  10. public class CheckBoxActivity extends Activity implements  
  11.         OnCheckedChangeListener {  
  12.     private CheckBox cb1, cb2, cb3;  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         cb1 = (CheckBox) findViewById(R.id.shanghai);  
  19.         cb2 = (CheckBox) findViewById(R.id.beijing);  
  20.         cb3 = (CheckBox) findViewById(R.id.tianjing);  
  21.         cb1.setOnCheckedChangeListener(this);  
  22.         cb2.setOnCheckedChangeListener(this);  
  23.         cb3.setOnCheckedChangeListener(this);  
  24.     }  
  25.   
  26.     @Override  
  27.         public void onCheckedChanged(CompoundButton buttonView,   //buttonView表示改变的框,isChecked表示是选中还是取消选中  
  28.                 boolean isChecked) {  
  29.             if(buttonView==cb1||buttonView==cb2||buttonView==cb3){  
  30.                 if(isChecked){  
  31.                     Toast.makeText(this, buttonView.getText()+"被选中",Toast.LENGTH_SHORT).show();  
  32.                 }  
  33.                 else{  
  34.                     Toast.makeText(this, buttonView.getText()+"取消选中",Toast.LENGTH_SHORT).show();  
  35.                 }  
  36.             }  
  37.           
  38.           
  39.     }  
  40. }</span></pre>  
  41. <pre></pre>  
  42. <pre></pre>  
  43. <pre></pre>  
  44. <pre></pre>  
  45. <pre></pre>  

5.RadioButton


单选框,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><RadioGroup>   
  2.     <RadioButton  
  3.             android:id="@+id/rb1"  
  4.             android:layout_width="fill_parent"  
  5.             android:layout_height="wrap_content"  
  6.             android:text="RadioButton1" >  
  7.     </RadioButton>  
  8.     <RadioButton>  
  9.     </RadioButton>  
  10.     ......  
  11. </RadioGroup></span>  

在单选框中也存在一个OnCheckedChangeListener,但是不同于多选框的监听器,虽然名字一样,但是所在包不一样。

代码示例:实现“男、女”单选框;

[html] view plaincopy
  1. <p><strong><span style="font-family:'Microsoft YaHei';">  
  2. 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"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RadioGroup  
  9.         android:id="@+id/rg1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content" >  
  12.   
  13.         <RadioButton  
  14.             android:id="@+id/rb1"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="男" >  
  18.         </RadioButton>  
  19.   
  20.         <RadioButton  
  21.             android:id="@+id/rb2"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="女" >  
  25.         </RadioButton>  
  26.     </RadioGroup>  
  27.   
  28. </LinearLayout></span></pre>  
  29. <pre></pre>  
  30. <span style="font-family:'Microsoft YaHei'"></span>  
  31. <pre></pre>  
  32. <pre></pre>  
  33. <pre></pre>  
  34. <pre></pre>  
[html] view plaincopy
  1. <p><strong><span style="font-family:'Microsoft YaHei';">RadioButtonActivity.java  
  2.   
  3. </span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.RadioButton;  
  8. import android.widget.RadioGroup;  
  9. import android.widget.Toast;  
  10. import android.widget.RadioGroup.OnCheckedChangeListener;  
  11.   
  12. public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{  
  13.     /** Called when the activity is first created. */  
  14.     private RadioButton rb1,rb2;  
  15.     private RadioGroup rg;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         rb1 = (RadioButton)findViewById(R.id.rb1);  
  21.         rb2 = (RadioButton)findViewById(R.id.rb2);  
  22.         rg = (RadioGroup)findViewById(R.id.rg1);  
  23.         rg.setOnCheckedChangeListener(this);  
  24.     }  
  25.     @Override  
  26.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  27.         if(group==rg){  
  28.             if(rb1.getId()==checkedId){  
  29.                 Toast.makeText(this, rb1.getText(), Toast.LENGTH_SHORT).show();  
  30.             }  
  31.             if(rb2.getId()==checkedId){  
  32.                 Toast.makeText(this, rb2.getText(), Toast.LENGTH_SHORT).show();  
  33.             }  
  34.         }  
  35.     }  
  36. }</span></pre>  
  37. <pre></pre>  
  38. <span style="font-family:'Microsoft YaHei'"></span>  
  39. <pre></pre>  
  40. <pre></pre>  
  41. <pre></pre>  
  42. <pre></pre>  


6.ProgressBar


进度条,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><ProgressBar  
  2.         android:id="@+id/pb1"  
  3.         style="?android:attr/progressBarStyleXxx"    <!--设置进度条的样式,有大、中、小、条状 -->  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content" /></span>  

1. ?andtroid:attr/progressBarStyleSmall圆形小进度条,动态
2. 默认,即不设置 圆形中等进度条,动态
3. ?android:attr/progressBarStyleLarge 圆形大进度条,动态
4. ?android:attr/progressBarStyleHorizontal 条状进度条,静态

条状进度条属性:


android:max
android:progress
android:secondaryProgress 

代码示例:实现条状进度条,并当安装结束时,跳出提示


[html] view plaincopy
  1. <p><strong><span style="font-family:'Microsoft YaHei';">  
  2. 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"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.     <ProgressBar  
  8.         android:id="@+id/pb4"  
  9.         style="?android:attr/progressBarStyleHorizontal"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:max="100"  
  13.         android:progress="0"  
  14.         android:secondaryProgress="0" />  
  15. </LinearLayout></span></pre>  
  16. <pre></pre>  
  17. <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;  
  18.   
  19. import android.app.Activity;  
  20. import android.os.Bundle;  
  21. import android.os.Handler;  
  22. import android.widget.ProgressBar;  
  23. import android.widget.Toast;  
  24.   
  25. public class ProgressBarActivity extends Activity implements Runnable {  
  26.     private ProgressBar bar;  
  27.     private boolean isFinished;  
  28.     Thread t;  
  29.     Handler handler = new Handler();  
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.         bar = (ProgressBar) findViewById(R.id.pb4);  
  35.         t = new Thread(this);  
  36.         t.start();  
  37.     }  
  38.     public void showToast() {  
  39.         handler.post(new Runnable() {  
  40.             @Override  
  41.             public void run() {  
  42.                 Toast.makeText(getApplicationContext(), "安装完成!",        //此处需要使用Handler,因为不能在子线程中使用Toast  
  43.                         Toast.LENGTH_SHORT).show();  
  44.             }  
  45.         });  
  46.     }  
  47.     public void run() {  
  48.         int current = bar.getProgress();  
  49.         int currentMax = bar.getMax();  
  50.         int secCurrent = bar.getSecondaryProgress();  
  51.         while (true) {  
  52.             bar.setProgress(current++);  
  53.             bar.setSecondaryProgress(secCurrent++);  
  54.             if (secCurrent >= currentMax) {  
  55.                 break;  
  56.             }  
  57.             try {  
  58.                 Thread.sleep(50);  
  59.             } catch (InterruptedException e) {  
  60.                 e.printStackTrace();  
  61.             }  
  62.         }  
  63.         isFinished = true;  
  64.         showToast();  
  65.     }  
  66. }</span></pre>  
  67. <pre></pre>  
  68. <h2><a name="t16"></a><span style="font-family:'Microsoft YaHei'">7.TextView</span></h2>  
  69. <span style="font-family:'Microsoft YaHei'">文本显示组件,在main.xml中定义如下:</span>  
  70. <pre></pre>  
  71. <pre></pre>  
  72. <pre></pre>  
  73. <pre></pre>  
  74. <pre></pre>  
  75. <pre></pre>  
  76. <pre></pre>  
  77. <pre></pre>  
  78. </pre>  


[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"> <TextView  
  2.         android:layout_width="wrap_content"  
  3.         android:layout_height="wrap_content"  
  4.         android:text="@string/hello" />    <!--文本文字 --></span>  

8.Dialog


对话框,不需要再main.xml中显示,只需要直接在Activity中创建即可; 

(1)简单的Dialog:

常用函数:
setMessage()
setTitle()
setIcon()
setPositiveButton()
setNegativeButton()

[java] view plaincopy
  1. Builder builder = new Builder(DialogActivity.this);   //创建对话框  
  2. builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("对话框标题");   //设置对话框图标和标题  
  3. builder.setMessage("对话框内容");    //设置对话框信息  
  4. builder.setPositiveButton("Yes"new OnClickListener(){      //设置正确按钮  
  5.         @Override  
  6.         public void onClick(DialogInterface dialog, int arg1) {  
  7.               
  8.         }  
  9. });  
  10. builder.setNegativeButton("No"new OnClickListener(){     //设置否定按钮  
  11.         @Override  
  12.         public void onClick(DialogInterface dialog, int arg1) {  
  13.                   
  14.         }  
  15. });  
  16. builder.show();     //显示对话框  

(2)在dialog中添加单选框和复选框:

实例:添加“上海、北京、天津”的多选框

setMultiChoiceItems();
setSingleChoiceItems(); 
注:设置这些和setMessage不能同时使用!

[html] view plaincopy
  1. <pre class="java" name="code">package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog.Builder;  
  5. import android.content.DialogInterface;  
  6. import android.content.DialogInterface.OnMultiChoiceClickListener;  
  7. import android.os.Bundle;  
  8.   
  9. public class DialogActivity extends Activity {  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         Builder builder = new Builder(DialogActivity.this);  
  14.         builder.setMultiChoiceItems(new String[] { "上海", "北京", "天津" },   //每项内容  
  15.                 new boolean[] { true, false, true },    //每项是否没选中  
  16.                 new OnMultiChoiceClickListener() {    //监听器  
  17.                     @Override  
  18.                     public void onClick(DialogInterface dialog, int which,  
  19.                             boolean isChecked) {  
  20.                           
  21.                     }  
  22.                 }).show();  
  23.     }  
  24. }</pre>  
  25. <pre></pre>  
  26. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  27. <div><span style="font-family:'Microsoft YaHei'"><strong>(3)在dialog中添加列表</strong></span></div>  
  28. <div><span style="font-family:'Microsoft YaHei'"><strong></strong></span></div>  
  29. <div><span style="font-family:'Microsoft YaHei'">builder.setItems(new String[]{"项1","项2"},new OnClickListener(){});  </span></div>  
  30. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  31. <div><span style="font-family:'Microsoft YaHei'"><strong>(4)在dialog中添加视图(在main.xml中定义):</strong></span></div>  
  32. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  33. setView函数实现;<pre class="java" name="code">        Builder builder = new Builder(DialogActivity.this);  
  34.         View layout = LayoutInflater.from(this).inflate(R.layout.main, null);  
  35.         builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("对话框标题");  
  36.         builder.setMessage("对话框内容");  
  37.         builder.setPositiveButton("Yes", new OnClickListener(){  
  38.             @Override  
  39.             public void onClick(DialogInterface dialog, int arg1) {  
  40.                   
  41.             }  
  42.         });  
  43.         builder.setNegativeButton("Yes", new OnClickListener(){  
  44.             @Override  
  45.             public void onClick(DialogInterface dialog, int arg1) {  
  46.                   
  47.             }  
  48.         });  
  49.         builder.setView(layout);  
  50.         builder.show();  
  51. </pre>  
  52. <pre></pre>  
  53. <pre></pre>  
  54. <pre></pre>  
  55. <pre></pre>  

9.TabHost


分页组件,类似于如下图:



在main.xml中无需定义,直接在TabActivity中创建即可,但是TabSpec中的具体内容需要自定义,即引用布局文件中的ID;
注:
(1)Activity需要继承TabActivity 而不是Activity;
(2)OnTabChangedListener为TabHost的监听器,存在方法:public void onTagChanged(String tabId);

(3)TabSpec t1 = tabHost.newTabSpec("TabID");
(4)t1.setContent(布局或控件id);   //为tabSpec添加某个布局
(5)t1.setIndicator(tab的标题);

代码示例:设置三页,每页有各自的内容

[html] view plaincopy
  1. <p><strong>main.xml</strong></p><p></p><pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/main"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/l1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="第1页"></TextView>  
  18.     </LinearLayout>  
  19.   
  20.     <LinearLayout  
  21.         android:id="@+id/l2"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:orientation="vertical" >  
  25.   
  26.         <TextView  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="第2页"></TextView>  
  30.     </LinearLayout>  
  31.   
  32.     <LinearLayout  
  33.         android:id="@+id/l3"  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:orientation="vertical" >  
  37.   
  38.         <TextView  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="第3页"></TextView>  
  42.     </LinearLayout>  
  43.   
  44. </LinearLayout></pre>  
  45. <pre></pre>  
  46. <pre class="html" name="code"><p><strong>TabHostActivity.java</strong></p>  
  47. <pre class="java" name="code">package org.xiazdong;  
  48.   
  49. import android.app.TabActivity;  
  50. import android.os.Bundle;  
  51. import android.util.Log;  
  52. import android.view.LayoutInflater;  
  53. import android.widget.TabHost;  
  54. import android.widget.TabHost.OnTabChangeListener;  
  55. import android.widget.TabHost.TabSpec;  
  56. import android.widget.Toast;  
  57.   
  58. public class TabHostActivity extends TabActivity implements OnTabChangeListener {   //继承TabActivity而不是Activity  
  59.     TabHost host;  
  60.   
  61.     @Override  
  62.     public void onCreate(Bundle savedInstanceState) {  
  63.         super.onCreate(savedInstanceState);  
  64.         host = this.getTabHost();    //新建TabHost  
  65.         LayoutInflater.from(this).inflate(R.layout.main,    //将main布局文件映射成tabHost的view  
  66.                 host.getTabContentView());  
  67.         TabSpec t1 = host.newTabSpec("t1");   //新建一个页,id为t1  
  68.         t1.setIndicator("标签1");  //设置显示页名  
  69.         t1.setContent(R.id.l1);    //设置页的内容为l1布局,此处可以是布局或组件  
  70.         host.addTab(t1);     //加入TabHost中  
  71.         TabSpec t2 = host.newTabSpec("t2");  
  72.         t2.setIndicator("标签2",getResources().getDrawable(R.drawable.ic_launcher));  
  73.         t2.setContent(R.id.l2);  
  74.         host.addTab(t2);  
  75.         TabSpec t3 = host.newTabSpec("t3");  
  76.         t3.setIndicator("标签3");  
  77.         t3.setContent(R.id.l3);  
  78.         host.addTab(t3);  
  79.         host.setOnTabChangedListener(this);   //设置监听器  
  80.     }  
  81.   
  82.     @Override  
  83.     public void onTabChanged(String tabId) {  
  84.         Log.v("a","aaaa");  
  85.         if(tabId.equals("t1")){  
  86.             Toast.makeText(this, "标签1ing", Toast.LENGTH_LONG).show();  
  87.         }  
  88.         if(tabId.equals("t2")){  
  89.             Toast.makeText(this, "标签2ing", Toast.LENGTH_LONG).show();  
  90.         }  
  91.         if(tabId.equals("t3")){  
  92.             Toast.makeText(this, "标签3ing", Toast.LENGTH_LONG).show();  
  93.         }  
  94.         else{  
  95.             Toast.makeText(this, tabId, Toast.LENGTH_LONG).show();  
  96.         }  
  97.     }  
  98. }</pre>  
  99. <pre></pre>  
  100. <pre></pre>  
  101. <pre></pre>  
  102. <pre></pre>  
  103. <pre></pre>  
  104. <pre></pre>  
  105. <pre></pre>  
  106. <pre></pre>  
  107. <pre></pre>  
  108. </pre>  

10.SeekBar


拖动条,在main.xml中定义如下:
[java] view plaincopy
  1. <SeekBar   
  2. android:id="@+id/sb"  
  3. android:layout_width="fill_parent"  
  4. android:layout_height="wrap_content"  
  5. />  

注:存在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中显示当前值

[html] view plaincopy
  1. <p><strong>main.xml</strong></p><p></p><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello"  
  11.         android:id="@+id/tv"  
  12.          />  
  13.     <SeekBar   
  14.         android:id="@+id/sb"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.           
  18.         />  
  19. </LinearLayout></pre>  
  20. <pre></pre>  
  21. <pre class="html" name="code"><p><strong>SeekBarActivity.java</strong></p><p></p><pre class="java" name="code">package org.xiazdong;  
  22.   
  23. import android.app.Activity;  
  24. import android.os.Bundle;  
  25. import android.widget.SeekBar;  
  26. import android.widget.SeekBar.OnSeekBarChangeListener;  
  27. import android.widget.TextView;  
  28.   
  29. public class SeekBarActivity extends Activity {  
  30.     private TextView tv;  
  31.     private SeekBar sb;  
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.         tv = (TextView) findViewById(R.id.tv);  
  37.         sb = (SeekBar) findViewById(R.id.sb);  
  38.         sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){  
  39.             @Override  
  40.             public void onProgressChanged(SeekBar seekBar, int progress,  
  41.                     boolean fromUser) {  
  42.                 tv.setText(progress+"");  
  43.             }  
  44.             @Override  
  45.             public void onStartTrackingTouch(SeekBar seekBar) {  
  46.             }  
  47.             @Override  
  48.             public void onStopTrackingTouch(SeekBar seekBar) {  
  49.             }  
  50.         });  
  51.     }  
  52. }</pre>  
  53. <pre></pre>  
  54. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  55. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  56. <h2><a name="t22"></a><span style="font-family:'Microsoft YaHei'">11.ListView</span></h2>  
  57. <div><span style="font-family:'Microsoft YaHei'">列表视图;</span></div>  
  58. <h3><a name="t23"></a><span style="font-family:'Microsoft YaHei'">(1)使用ArrayAdapter实现普通列表</span></h3>  
  59. <div><span style="font-family:'Microsoft YaHei'">ArrayAdapter是一个媒介,通过它可以把数组映射到ListView视图上。</span></div>  
  60. <div>(1)new ArrayAapter<String>(this,android.R.layout.simple_list_item_1,list);   将list存放到ArrayAdapter中;</div>  
  61. <div>(2)lv.setAdapter(adapter);  为listView设置Adapter;</div>  
  62. <div><span style="font-family:'Microsoft YaHei'"></span><pre class="java" name="code">package org.xiazdong;  
  63.   
  64. import java.util.ArrayList;  
  65.   
  66. import android.app.Activity;  
  67. import android.os.Bundle;  
  68. import android.view.View;  
  69. import android.widget.AdapterView;  
  70. import android.widget.ArrayAdapter;  
  71. import android.widget.ListView;  
  72. import android.widget.Toast;  
  73. import android.widget.AdapterView.OnItemClickListener;  
  74.   
  75. public class ListViewActivity extends Activity implements OnItemClickListener{  
  76.     ArrayList<String> list;  
  77.     @Override  
  78.     public void onCreate(Bundle savedInstanceState) {  
  79.         super.onCreate(savedInstanceState);  
  80.         list = new ArrayList<String>();  
  81.         list.add("xiazdong-1");  
  82.         list.add("xiazdong-2");  
  83.         list.add("xiazdong-3");  
  84.         ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);  
  85.         ListView lv = new ListView(this);  
  86.         lv.setAdapter(adapter);  
  87.         lv.setOnItemClickListener(this);  
  88.         this.setContentView(lv);  
  89.     }  
  90.     @Override  
  91.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  92.         Toast.makeText(this,list.get(arg2), Toast.LENGTH_SHORT).show();  
  93.     }  
  94. }</pre><br>  
  95. <h3><a name="t24"></a><span style="font-family:'Microsoft YaHei'">(2)自定义适配器BaseAdapter</span></h3>  
  96. </div>  
  97. <div><br>  
  98. </div>  
  99. <br>  
  100. <h1><a name="t25"></a><span style="font-family:'Microsoft YaHei'">二、4种布局介绍</span></h1>  
  101. <div><span style="font-family:'Microsoft YaHei'"><br>  
  102. </span></div>  
  103. <span style="font-family:'Microsoft YaHei'">AbsoluteLayout因为已被废除,因此不做介绍;<br>  
  104. 只要存在界面,就会有布局的存在,就像Swing,虽然一个是桌面应用,一个是手机应用,但是他们都差不多。<br>  
  105. </span>  
  106. <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>  
  107. <br>  
  108. 此处因为布局非常简单,所以就不用代码来讲解了。<br>  
  109. <br>  
  110. <br>  
  111. <h2><a name="t27"></a><span style="font-family:'Microsoft YaHei'">1.LinearLayout</span></h2>  
  112. <span style="font-family:'Microsoft YaHei'"><br>  
  113. <br>  
  114. 默认布局。组件的排列按照预先定义方向很有序的排列,类似于Swing中的FlowLayout;<br>  
  115. 注意点:</span>  
  116. <pre></pre>  
  117. <pre></pre>  
  118. <pre></pre>  
  119. <pre></pre>  
  120. <pre></pre>  
  121. <pre></pre>  
  122. <pre></pre>  
  123. <pre></pre>  
  124. </pre>  
(1)可以在<LinearLayout>中添加android:orientation:vertical/horizontal ;
(2)可以嵌套<LinearLayout>;

2.FrameLayout


每个组件都在左上角,如果多个组件一起出现,则会重叠;

3.RelativeLayout


每个组件定位都是按照与其他组件的上下、左右定位;
默认的定位为左上方;
(1)定位与组件的上下左右
android:layout_below="@id/.."
android:layout_above="@id/"
android:layout_toLeftOf="@id/"
android:layout_toRightOf="@id/"
(2)定位与组件的边缘对齐
android:layout_alignLeft="@id/"
android:layout_alignRight="@id/"
android:layout_alignTop="@id/"
android:layout_alignBottom="@id/"
(3)定位与父组件的边缘对齐
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
(4)与整个屏幕的关系
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"

4.TableLayout


类似于Swing中的GridLayout;
表格布局的每行用<TabRow>括起来;
在<TableLayout>中可以定义如下属性:

(1)android:shrinkColumns="1" 表明第2个控件如果里面的内容过多,会收缩,扩展到第二行,而不是延伸;
(2)android:stretchColumns="2" 如果有空白,第3个控件填充;

在控件中设置:

(1)android:layout_column="2" 将此控件放在第3个位置;
(2)android:layout_span="2" 此控件占据2个单元位置;

补充:


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之间跳转


使用Intent进行多个页面的跳转;
(1)Intent intent = new Intent(Context c,Class class);  c表示当前界面,class表示要跳转到的界面的class;
(2)intent.putExtra(String key,String value);  //设置传输内容;
(3)this.startActivity(intent);   //开始跳转
(4)Intent intent = this.getIntent();  //获得传输来的intent
(5)String value = intent.getStringExtra(String key);   //获得数据

代码示例:


main.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第一个界面" />  
  11.         <TextView  
  12.         android:id="@+id/tv1"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="" />  
  16.     <EditText   
  17.         android:id="@+id/e1"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:hint="输入信息"  
  21.         />  
  22.     <Button   
  23.         android:id="@+id/b1"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="发送到第二个界面"  
  27.         />  
  28. </LinearLayout>  

mylayout.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第二个界面" />  
  11.         <TextView  
  12.         android:id="@+id/tv2"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="" />  
  16.     <EditText   
  17.         android:id="@+id/e2"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:hint="输入信息"  
  21.         />  
  22.     <Button   
  23.         android:id="@+id/b2"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="发送到第一个界面"  
  27.         />  
  28. </LinearLayout>  

MultiActivityActivity.java
[java] view plaincopy
  1. package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11.   
  12. public class MultiActivityActivity extends Activity implements OnClickListener{  
  13.     private Button b1;  
  14.     private EditText e1;  
  15.     private TextView tv1;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         b1 = (Button)findViewById(R.id.b1);  
  21.         e1 = (EditText)findViewById(R.id.e1);  
  22.         tv1 = (TextView)findViewById(R.id.tv1);  
  23.         Intent i = this.getIntent();  
  24.         if(i.getStringExtra("2")!=null){  
  25.             tv1.setText(i.getStringExtra("2"));  
  26.         }  
  27.         b1.setOnClickListener(this);  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onClick(View v) {  
  32.           
  33.         Intent intent = new Intent(MultiActivityActivity.this,OtherActivity.class);  
  34.         intent.putExtra("1", e1.getText().toString());  
  35.         this.startActivity(intent);  
  36.     }  
  37. }  

OtherActivity.java

[java] view plaincopy
  1. package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.DialogInterface;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.TextView;  
  12.   
  13. public class OtherActivity extends Activity implements OnClickListener{  
  14.     private TextView view ;  
  15.     private Button b2;  
  16.     private EditText e2;  
  17.     private TextView tv2;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         view = new TextView(this);  
  22.         setContentView(R.layout.mylayout);  
  23.         b2 = (Button)findViewById(R.id.b2);  
  24.         e2 = (EditText)findViewById(R.id.e2);  
  25.         tv2 = (TextView)findViewById(R.id.tv2);  
  26.         Intent i = this.getIntent();  
  27.         if(i.getStringExtra("1")!=null){  
  28.             tv2.setText(i.getStringExtra("1"));  
  29.         }  
  30.         b2.setOnClickListener(this);  
  31.     }  
  32.     @Override  
  33.     public void onClick(View v) {  
  34.           
  35.         Intent intent = new Intent(OtherActivity.this,MultiActivityActivity.class);  
  36.         intent.putExtra("2", e2.getText().toString());  
  37.         this.startActivity(intent);  
  38.     }  
  39.       
  40. }  



[html] view plaincopy
  1. <pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  

一、基本控件介绍


一般新建组件有两种方式:XML中定义和Java代码实现,一般XML中定义较为常用。

 

1.Button


按钮,在main.xml中定义如下:

 

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><Button   
  2. <span style="WHITE-SPACE: pre"> </span>android:layout_width="wrap_content"  <!--按钮宽度匹配文本的大小 -->  
  3.     android:layout_height="wrap_content"  <!--按钮高度匹配文本大小 -->  
  4.     android:text="文本"   <!--按钮的文本 -->  
  5.     android:id="@+id/button1"  <!--按钮的id -->  
  6. ></Button></span>  

Button的监听器:onClickListener;

需要实现方法:public void onClick(View v); v表示触发的控件,比如按钮

代码示例:实现点击按钮生成随机数;


ButtonActivity.java

[java] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  2. import java.util.Random;  
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12. public class ButtonActivity extends Activity implements OnClickListener{    //实现点击监听器  
  13.     private Button button;  
  14.     private TextView tv;  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         button = (Button)findViewById(R.id.button1);        //根据ID找组件  
  20.         tv = (TextView)findViewById(R.id.tv);  
  21.         button.setOnClickListener(this);        //为button设置监听器  
  22.     }  
  23.     @Override  
  24.     public void onClick(View view) {  
  25.         String str = new Random().nextInt()+"";  
  26.         tv.setText(str);  
  27.         Toast.makeText(this"点击了按钮!!", Toast.LENGTH_SHORT).show(); //设置提示信息  
  28.          Builder builder = new AlertDialog.Builder(this);           //创建对话框  
  29.          builder.setTitle("提示信息").setMessage("点击了按钮,随机数为:"+str).show(); //设置对话框属性并显示  
  30.     }  
  31. }</span>  


main.xml

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text=""   
  11.         android:id="@+id/tv"  
  12.         />  
  13.     <Button   
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="点击生成随机数"  
  17.         android:id="@+id/button1"  
  18.         ></Button>  
  19. </LinearLayout></span>  

2.ImageButton


和Button的区别为背景可以自定义图片,在main.xml中定义如下:

 

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><ImageButton   
  2.     android:layout_width="wrap_content"   
  3.     android:layout_height="wrap_content"  
  4.     android:id="@+id/ib1"  
  5.     android:background="@drawable/ic_launcher"/>  <!--设置按钮的背景为drawable文件夹下的ic_launcher图片 --></span>  


代码示例:实现点击图片按钮就切换图片;


main.xml
[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.     <ImageButton   
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/ib1"  
  15.         android:background="@drawable/ic_launcher"/>  
  16. </LinearLayout></span>  

ImageButtonActivity.java

[java] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.MotionEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnTouchListener;  
  8. import android.widget.ImageButton;  
  9.   
  10. public class ImageButtonActivity extends Activity {  
  11.     private ImageButton ib1;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         ib1 = (ImageButton) findViewById(R.id.ib1);  
  17.         ib1.setOnTouchListener(new OnTouchListener(){  
  18.   
  19.             @Override  
  20.             public boolean onTouch(View v, MotionEvent event) {  
  21.                 if(event.getAction()==MotionEvent.ACTION_DOWN){  //按下按钮时  
  22.                     ib1.setBackgroundResource(R.drawable.logo);  
  23.                 }  
  24.                 else if(event.getAction()==MotionEvent.ACTION_UP){  //抬起按钮时  
  25.                     ib1.setBackgroundResource(R.drawable.ic_launcher);  
  26.                 }  
  27.                 return false;  
  28.             }  
  29.               
  30.         });  
  31.     }  
  32. }</span>  

3.EditText


文本框,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><EditText  
  2.      android:id="@+id/name"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="wrap_content"  
  5.      android:hint="输入用户名..."   
  6.      android:inputType=""  
  7. /></span>  

可以在<EditText>中设置以下属性:
(1)android:inputType="number":输入类型为数字;

(2)android:maxLength="2":输入最长为2;

(3)android:singleLine="true":只能单行显示;

(4)android:password="true" :输入的形式为密码

(5)android:numeric="integer":输入整数

代码示例:实现用户登录;


[html] view plaincopy
  1. <p><span style="font-family:'Microsoft YaHei';"><strong>main.xml</strong>  
  2. </span></p><pre class="html" name="code"><span style="font-family:'Microsoft YaHei';"><?xml version="1.0" encoding="utf-8"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         xmlns:android="http://schemas.android.com/apk/res/android"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="用户名:" />  
  18.   
  19.         <EditText  
  20.             android:id="@+id/name"  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:hint="输入用户名..."   
  24.             android:inputType=""  
  25.             />  
  26.     </LinearLayout>  
  27.   
  28.     <LinearLayout  
  29.         xmlns:android="http://schemas.android.com/apk/res/android"  
  30.         android:layout_width="fill_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:orientation="horizontal" >  
  33.  <TextView  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:text="密码:" />  
  37.         <EditText  
  38.             android:id="@+id/password"  
  39.             android:layout_width="fill_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:hint="输入密码..."  
  42.             android:password="true" />  
  43. </LinearLayout>  
  44.         <Button  
  45.             android:id="@+id/button"  
  46.             android:layout_width="fill_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:text="提交" >  
  49.         </Button>  
  50.     </LinearLayout>  
  51. </span></pre>  
  52. <pre></pre>  
  53. <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;  
  54.   
  55. import android.app.Activity;  
  56. import android.app.AlertDialog;  
  57. import android.app.AlertDialog.Builder;  
  58. import android.content.DialogInterface;  
  59. import android.os.Bundle;  
  60. import android.view.View;  
  61. import android.view.View.OnClickListener;  
  62. import android.widget.Button;  
  63. import android.widget.EditText;  
  64.   
  65. public class EditTextActivity extends Activity {  
  66.     private EditText name;  
  67.     private EditText password;  
  68.     private Button button;  
  69.   
  70.     @Override  
  71.     public void onCreate(Bundle savedInstanceState) {  
  72.         super.onCreate(savedInstanceState);  
  73.         setContentView(R.layout.main);  
  74.         name = (EditText) findViewById(R.id.name);  
  75.         button = (Button) findViewById(R.id.button);  
  76.         password = (EditText) findViewById(R.id.password);  
  77.         button.setOnClickListener(new OnClickListener() {  
  78.             @Override  
  79.             public void onClick(View v) {  
  80.                 String n = name.getText().toString();  
  81.                 String p = password.getText().toString();  
  82.                 Builder builder = new AlertDialog.Builder(EditTextActivity.this); // 创建对话框  
  83.                 builder.setTitle("提示信息").setMessage("用户名:" + n + "\n密码:" + p)  
  84.                         .setPositiveButton("知道了", new DialogInterface.OnClickListener() {  
  85.                             @Override  
  86.                             public void onClick(DialogInterface dialog, int which) {  
  87.                                 password.setText("");   //清空密码  
  88.                             }  
  89.                         }).show();                      // 设置对话框属性并显示  
  90.             }  
  91.         });  
  92.     }  
  93. }</span></pre><pre class="java" name="code"><span style="font-family:微软雅黑;"></span> </pre>  
  94. <pre></pre>  
  95. <span style="font-family:'Microsoft YaHei'"></span>  
  96. <pre></pre>  
  97. <pre></pre>  
  98. <pre></pre>  
  99. <pre></pre>  
  100. <pre></pre>  
  101. <pre></pre>  
  102. <pre></pre>  
  103. <pre></pre>  
  104. </pre>  

4.CheckBox


多选框,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"> <CheckBox  
  2.         android:id="@+id/shanghai"  
  3.         android:layout_width="wrap_content"  
  4.         android:layout_height="wrap_content"  
  5.         android:text="" /></span>  

onCheckedChangeListener监听器是专门对CheckBox进行监听,实现方法:public void onCheckedChanged(CompundButton buttonView,boolean isChecked);

代码示例:实现上海、北京、天津的复选框

[html] view plaincopy
  1. <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"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="城市:" />  
  11.   
  12.     <CheckBox  
  13.         android:id="@+id/shanghai"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="上海" />  
  17.   
  18.     <CheckBox  
  19.         android:id="@+id/beijing"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="北京" />  
  23.   
  24.     <CheckBox  
  25.         android:id="@+id/tianjing"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="天津" />  
  29.   
  30. </LinearLayout></span></pre>  
  31. <pre></pre>  
  32. <span style="font-family:'Microsoft YaHei'"></span>  
  33. <pre></pre>  
  34. <pre></pre>  
  35. <pre></pre>  
  36. <pre></pre>  
[html] view plaincopy
  1. <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;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.CheckBox;  
  6. import android.widget.CompoundButton;  
  7. import android.widget.CompoundButton.OnCheckedChangeListener;  
  8. import android.widget.Toast;  
  9.   
  10. public class CheckBoxActivity extends Activity implements  
  11.         OnCheckedChangeListener {  
  12.     private CheckBox cb1, cb2, cb3;  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         cb1 = (CheckBox) findViewById(R.id.shanghai);  
  19.         cb2 = (CheckBox) findViewById(R.id.beijing);  
  20.         cb3 = (CheckBox) findViewById(R.id.tianjing);  
  21.         cb1.setOnCheckedChangeListener(this);  
  22.         cb2.setOnCheckedChangeListener(this);  
  23.         cb3.setOnCheckedChangeListener(this);  
  24.     }  
  25.   
  26.     @Override  
  27.         public void onCheckedChanged(CompoundButton buttonView,   //buttonView表示改变的框,isChecked表示是选中还是取消选中  
  28.                 boolean isChecked) {  
  29.             if(buttonView==cb1||buttonView==cb2||buttonView==cb3){  
  30.                 if(isChecked){  
  31.                     Toast.makeText(this, buttonView.getText()+"被选中",Toast.LENGTH_SHORT).show();  
  32.                 }  
  33.                 else{  
  34.                     Toast.makeText(this, buttonView.getText()+"取消选中",Toast.LENGTH_SHORT).show();  
  35.                 }  
  36.             }  
  37.           
  38.           
  39.     }  
  40. }</span></pre>  
  41. <pre></pre>  
  42. <pre></pre>  
  43. <pre></pre>  
  44. <pre></pre>  
  45. <pre></pre>  

5.RadioButton


单选框,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><RadioGroup>   
  2.     <RadioButton  
  3.             android:id="@+id/rb1"  
  4.             android:layout_width="fill_parent"  
  5.             android:layout_height="wrap_content"  
  6.             android:text="RadioButton1" >  
  7.     </RadioButton>  
  8.     <RadioButton>  
  9.     </RadioButton>  
  10.     ......  
  11. </RadioGroup></span>  

在单选框中也存在一个OnCheckedChangeListener,但是不同于多选框的监听器,虽然名字一样,但是所在包不一样。

代码示例:实现“男、女”单选框;

[html] view plaincopy
  1. <p><strong><span style="font-family:'Microsoft YaHei';">  
  2. 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"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RadioGroup  
  9.         android:id="@+id/rg1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content" >  
  12.   
  13.         <RadioButton  
  14.             android:id="@+id/rb1"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="男" >  
  18.         </RadioButton>  
  19.   
  20.         <RadioButton  
  21.             android:id="@+id/rb2"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="女" >  
  25.         </RadioButton>  
  26.     </RadioGroup>  
  27.   
  28. </LinearLayout></span></pre>  
  29. <pre></pre>  
  30. <span style="font-family:'Microsoft YaHei'"></span>  
  31. <pre></pre>  
  32. <pre></pre>  
  33. <pre></pre>  
  34. <pre></pre>  
[html] view plaincopy
  1. <p><strong><span style="font-family:'Microsoft YaHei';">RadioButtonActivity.java  
  2.   
  3. </span></strong></p><p></p><pre class="java" name="code"><span style="font-family:'Microsoft YaHei';">package org.xiazdong;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.RadioButton;  
  8. import android.widget.RadioGroup;  
  9. import android.widget.Toast;  
  10. import android.widget.RadioGroup.OnCheckedChangeListener;  
  11.   
  12. public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{  
  13.     /** Called when the activity is first created. */  
  14.     private RadioButton rb1,rb2;  
  15.     private RadioGroup rg;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         rb1 = (RadioButton)findViewById(R.id.rb1);  
  21.         rb2 = (RadioButton)findViewById(R.id.rb2);  
  22.         rg = (RadioGroup)findViewById(R.id.rg1);  
  23.         rg.setOnCheckedChangeListener(this);  
  24.     }  
  25.     @Override  
  26.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
  27.         if(group==rg){  
  28.             if(rb1.getId()==checkedId){  
  29.                 Toast.makeText(this, rb1.getText(), Toast.LENGTH_SHORT).show();  
  30.             }  
  31.             if(rb2.getId()==checkedId){  
  32.                 Toast.makeText(this, rb2.getText(), Toast.LENGTH_SHORT).show();  
  33.             }  
  34.         }  
  35.     }  
  36. }</span></pre>  
  37. <pre></pre>  
  38. <span style="font-family:'Microsoft YaHei'"></span>  
  39. <pre></pre>  
  40. <pre></pre>  
  41. <pre></pre>  
  42. <pre></pre>  


6.ProgressBar


进度条,在main.xml中定义如下:

[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"><ProgressBar  
  2.         android:id="@+id/pb1"  
  3.         style="?android:attr/progressBarStyleXxx"    <!--设置进度条的样式,有大、中、小、条状 -->  
  4.         android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content" /></span>  

1. ?andtroid:attr/progressBarStyleSmall圆形小进度条,动态
2. 默认,即不设置 圆形中等进度条,动态
3. ?android:attr/progressBarStyleLarge 圆形大进度条,动态
4. ?android:attr/progressBarStyleHorizontal 条状进度条,静态

条状进度条属性:


android:max
android:progress
android:secondaryProgress 

代码示例:实现条状进度条,并当安装结束时,跳出提示


[html] view plaincopy
  1. <p><strong><span style="font-family:'Microsoft YaHei';">  
  2. 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"?>  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.     <ProgressBar  
  8.         android:id="@+id/pb4"  
  9.         style="?android:attr/progressBarStyleHorizontal"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:max="100"  
  13.         android:progress="0"  
  14.         android:secondaryProgress="0" />  
  15. </LinearLayout></span></pre>  
  16. <pre></pre>  
  17. <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;  
  18.   
  19. import android.app.Activity;  
  20. import android.os.Bundle;  
  21. import android.os.Handler;  
  22. import android.widget.ProgressBar;  
  23. import android.widget.Toast;  
  24.   
  25. public class ProgressBarActivity extends Activity implements Runnable {  
  26.     private ProgressBar bar;  
  27.     private boolean isFinished;  
  28.     Thread t;  
  29.     Handler handler = new Handler();  
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.         bar = (ProgressBar) findViewById(R.id.pb4);  
  35.         t = new Thread(this);  
  36.         t.start();  
  37.     }  
  38.     public void showToast() {  
  39.         handler.post(new Runnable() {  
  40.             @Override  
  41.             public void run() {  
  42.                 Toast.makeText(getApplicationContext(), "安装完成!",        //此处需要使用Handler,因为不能在子线程中使用Toast  
  43.                         Toast.LENGTH_SHORT).show();  
  44.             }  
  45.         });  
  46.     }  
  47.     public void run() {  
  48.         int current = bar.getProgress();  
  49.         int currentMax = bar.getMax();  
  50.         int secCurrent = bar.getSecondaryProgress();  
  51.         while (true) {  
  52.             bar.setProgress(current++);  
  53.             bar.setSecondaryProgress(secCurrent++);  
  54.             if (secCurrent >= currentMax) {  
  55.                 break;  
  56.             }  
  57.             try {  
  58.                 Thread.sleep(50);  
  59.             } catch (InterruptedException e) {  
  60.                 e.printStackTrace();  
  61.             }  
  62.         }  
  63.         isFinished = true;  
  64.         showToast();  
  65.     }  
  66. }</span></pre>  
  67. <pre></pre>  
  68. <h2><a name="t16"></a><span style="font-family:'Microsoft YaHei'">7.TextView</span></h2>  
  69. <span style="font-family:'Microsoft YaHei'">文本显示组件,在main.xml中定义如下:</span>  
  70. <pre></pre>  
  71. <pre></pre>  
  72. <pre></pre>  
  73. <pre></pre>  
  74. <pre></pre>  
  75. <pre></pre>  
  76. <pre></pre>  
  77. <pre></pre>  
  78. </pre>  


[html] view plaincopy
  1. <span style="font-family:'Microsoft YaHei';"> <TextView  
  2.         android:layout_width="wrap_content"  
  3.         android:layout_height="wrap_content"  
  4.         android:text="@string/hello" />    <!--文本文字 --></span>  

8.Dialog


对话框,不需要再main.xml中显示,只需要直接在Activity中创建即可; 

(1)简单的Dialog:

常用函数:
setMessage()
setTitle()
setIcon()
setPositiveButton()
setNegativeButton()

[java] view plaincopy
  1. Builder builder = new Builder(DialogActivity.this);   //创建对话框  
  2. builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("对话框标题");   //设置对话框图标和标题  
  3. builder.setMessage("对话框内容");    //设置对话框信息  
  4. builder.setPositiveButton("Yes"new OnClickListener(){      //设置正确按钮  
  5.         @Override  
  6.         public void onClick(DialogInterface dialog, int arg1) {  
  7.               
  8.         }  
  9. });  
  10. builder.setNegativeButton("No"new OnClickListener(){     //设置否定按钮  
  11.         @Override  
  12.         public void onClick(DialogInterface dialog, int arg1) {  
  13.                   
  14.         }  
  15. });  
  16. builder.show();     //显示对话框  

(2)在dialog中添加单选框和复选框:

实例:添加“上海、北京、天津”的多选框

setMultiChoiceItems();
setSingleChoiceItems(); 
注:设置这些和setMessage不能同时使用!

[html] view plaincopy
  1. <pre class="java" name="code">package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog.Builder;  
  5. import android.content.DialogInterface;  
  6. import android.content.DialogInterface.OnMultiChoiceClickListener;  
  7. import android.os.Bundle;  
  8.   
  9. public class DialogActivity extends Activity {  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         Builder builder = new Builder(DialogActivity.this);  
  14.         builder.setMultiChoiceItems(new String[] { "上海", "北京", "天津" },   //每项内容  
  15.                 new boolean[] { true, false, true },    //每项是否没选中  
  16.                 new OnMultiChoiceClickListener() {    //监听器  
  17.                     @Override  
  18.                     public void onClick(DialogInterface dialog, int which,  
  19.                             boolean isChecked) {  
  20.                           
  21.                     }  
  22.                 }).show();  
  23.     }  
  24. }</pre>  
  25. <pre></pre>  
  26. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  27. <div><span style="font-family:'Microsoft YaHei'"><strong>(3)在dialog中添加列表</strong></span></div>  
  28. <div><span style="font-family:'Microsoft YaHei'"><strong></strong></span></div>  
  29. <div><span style="font-family:'Microsoft YaHei'">builder.setItems(new String[]{"项1","项2"},new OnClickListener(){});  </span></div>  
  30. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  31. <div><span style="font-family:'Microsoft YaHei'"><strong>(4)在dialog中添加视图(在main.xml中定义):</strong></span></div>  
  32. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  33. setView函数实现;<pre class="java" name="code">        Builder builder = new Builder(DialogActivity.this);  
  34.         View layout = LayoutInflater.from(this).inflate(R.layout.main, null);  
  35.         builder.setIcon(android.R.drawable.ic_dialog_info).setTitle("对话框标题");  
  36.         builder.setMessage("对话框内容");  
  37.         builder.setPositiveButton("Yes", new OnClickListener(){  
  38.             @Override  
  39.             public void onClick(DialogInterface dialog, int arg1) {  
  40.                   
  41.             }  
  42.         });  
  43.         builder.setNegativeButton("Yes", new OnClickListener(){  
  44.             @Override  
  45.             public void onClick(DialogInterface dialog, int arg1) {  
  46.                   
  47.             }  
  48.         });  
  49.         builder.setView(layout);  
  50.         builder.show();  
  51. </pre>  
  52. <pre></pre>  
  53. <pre></pre>  
  54. <pre></pre>  
  55. <pre></pre>  

9.TabHost


分页组件,类似于如下图:



在main.xml中无需定义,直接在TabActivity中创建即可,但是TabSpec中的具体内容需要自定义,即引用布局文件中的ID;
注:
(1)Activity需要继承TabActivity 而不是Activity;
(2)OnTabChangedListener为TabHost的监听器,存在方法:public void onTagChanged(String tabId);

(3)TabSpec t1 = tabHost.newTabSpec("TabID");
(4)t1.setContent(布局或控件id);   //为tabSpec添加某个布局
(5)t1.setIndicator(tab的标题);

代码示例:设置三页,每页有各自的内容

[html] view plaincopy
  1. <p><strong>main.xml</strong></p><p></p><pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/main"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/l1"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="第1页"></TextView>  
  18.     </LinearLayout>  
  19.   
  20.     <LinearLayout  
  21.         android:id="@+id/l2"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:orientation="vertical" >  
  25.   
  26.         <TextView  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="第2页"></TextView>  
  30.     </LinearLayout>  
  31.   
  32.     <LinearLayout  
  33.         android:id="@+id/l3"  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:orientation="vertical" >  
  37.   
  38.         <TextView  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="第3页"></TextView>  
  42.     </LinearLayout>  
  43.   
  44. </LinearLayout></pre>  
  45. <pre></pre>  
  46. <pre class="html" name="code"><p><strong>TabHostActivity.java</strong></p>  
  47. <pre class="java" name="code">package org.xiazdong;  
  48.   
  49. import android.app.TabActivity;  
  50. import android.os.Bundle;  
  51. import android.util.Log;  
  52. import android.view.LayoutInflater;  
  53. import android.widget.TabHost;  
  54. import android.widget.TabHost.OnTabChangeListener;  
  55. import android.widget.TabHost.TabSpec;  
  56. import android.widget.Toast;  
  57.   
  58. public class TabHostActivity extends TabActivity implements OnTabChangeListener {   //继承TabActivity而不是Activity  
  59.     TabHost host;  
  60.   
  61.     @Override  
  62.     public void onCreate(Bundle savedInstanceState) {  
  63.         super.onCreate(savedInstanceState);  
  64.         host = this.getTabHost();    //新建TabHost  
  65.         LayoutInflater.from(this).inflate(R.layout.main,    //将main布局文件映射成tabHost的view  
  66.                 host.getTabContentView());  
  67.         TabSpec t1 = host.newTabSpec("t1");   //新建一个页,id为t1  
  68.         t1.setIndicator("标签1");  //设置显示页名  
  69.         t1.setContent(R.id.l1);    //设置页的内容为l1布局,此处可以是布局或组件  
  70.         host.addTab(t1);     //加入TabHost中  
  71.         TabSpec t2 = host.newTabSpec("t2");  
  72.         t2.setIndicator("标签2",getResources().getDrawable(R.drawable.ic_launcher));  
  73.         t2.setContent(R.id.l2);  
  74.         host.addTab(t2);  
  75.         TabSpec t3 = host.newTabSpec("t3");  
  76.         t3.setIndicator("标签3");  
  77.         t3.setContent(R.id.l3);  
  78.         host.addTab(t3);  
  79.         host.setOnTabChangedListener(this);   //设置监听器  
  80.     }  
  81.   
  82.     @Override  
  83.     public void onTabChanged(String tabId) {  
  84.         Log.v("a","aaaa");  
  85.         if(tabId.equals("t1")){  
  86.             Toast.makeText(this, "标签1ing", Toast.LENGTH_LONG).show();  
  87.         }  
  88.         if(tabId.equals("t2")){  
  89.             Toast.makeText(this, "标签2ing", Toast.LENGTH_LONG).show();  
  90.         }  
  91.         if(tabId.equals("t3")){  
  92.             Toast.makeText(this, "标签3ing", Toast.LENGTH_LONG).show();  
  93.         }  
  94.         else{  
  95.             Toast.makeText(this, tabId, Toast.LENGTH_LONG).show();  
  96.         }  
  97.     }  
  98. }</pre>  
  99. <pre></pre>  
  100. <pre></pre>  
  101. <pre></pre>  
  102. <pre></pre>  
  103. <pre></pre>  
  104. <pre></pre>  
  105. <pre></pre>  
  106. <pre></pre>  
  107. <pre></pre>  
  108. </pre>  

10.SeekBar


拖动条,在main.xml中定义如下:
[java] view plaincopy
  1. <SeekBar   
  2. android:id="@+id/sb"  
  3. android:layout_width="fill_parent"  
  4. android:layout_height="wrap_content"  
  5. />  

注:存在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中显示当前值

[html] view plaincopy
  1. <p><strong>main.xml</strong></p><p></p><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello"  
  11.         android:id="@+id/tv"  
  12.          />  
  13.     <SeekBar   
  14.         android:id="@+id/sb"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.           
  18.         />  
  19. </LinearLayout></pre>  
  20. <pre></pre>  
  21. <pre class="html" name="code"><p><strong>SeekBarActivity.java</strong></p><p></p><pre class="java" name="code">package org.xiazdong;  
  22.   
  23. import android.app.Activity;  
  24. import android.os.Bundle;  
  25. import android.widget.SeekBar;  
  26. import android.widget.SeekBar.OnSeekBarChangeListener;  
  27. import android.widget.TextView;  
  28.   
  29. public class SeekBarActivity extends Activity {  
  30.     private TextView tv;  
  31.     private SeekBar sb;  
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.         tv = (TextView) findViewById(R.id.tv);  
  37.         sb = (SeekBar) findViewById(R.id.sb);  
  38.         sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){  
  39.             @Override  
  40.             public void onProgressChanged(SeekBar seekBar, int progress,  
  41.                     boolean fromUser) {  
  42.                 tv.setText(progress+"");  
  43.             }  
  44.             @Override  
  45.             public void onStartTrackingTouch(SeekBar seekBar) {  
  46.             }  
  47.             @Override  
  48.             public void onStopTrackingTouch(SeekBar seekBar) {  
  49.             }  
  50.         });  
  51.     }  
  52. }</pre>  
  53. <pre></pre>  
  54. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  55. <div><span style="font-family:'Microsoft YaHei'"></span></div>  
  56. <h2><a name="t22"></a><span style="font-family:'Microsoft YaHei'">11.ListView</span></h2>  
  57. <div><span style="font-family:'Microsoft YaHei'">列表视图;</span></div>  
  58. <h3><a name="t23"></a><span style="font-family:'Microsoft YaHei'">(1)使用ArrayAdapter实现普通列表</span></h3>  
  59. <div><span style="font-family:'Microsoft YaHei'">ArrayAdapter是一个媒介,通过它可以把数组映射到ListView视图上。</span></div>  
  60. <div>(1)new ArrayAapter<String>(this,android.R.layout.simple_list_item_1,list);   将list存放到ArrayAdapter中;</div>  
  61. <div>(2)lv.setAdapter(adapter);  为listView设置Adapter;</div>  
  62. <div><span style="font-family:'Microsoft YaHei'"></span><pre class="java" name="code">package org.xiazdong;  
  63.   
  64. import java.util.ArrayList;  
  65.   
  66. import android.app.Activity;  
  67. import android.os.Bundle;  
  68. import android.view.View;  
  69. import android.widget.AdapterView;  
  70. import android.widget.ArrayAdapter;  
  71. import android.widget.ListView;  
  72. import android.widget.Toast;  
  73. import android.widget.AdapterView.OnItemClickListener;  
  74.   
  75. public class ListViewActivity extends Activity implements OnItemClickListener{  
  76.     ArrayList<String> list;  
  77.     @Override  
  78.     public void onCreate(Bundle savedInstanceState) {  
  79.         super.onCreate(savedInstanceState);  
  80.         list = new ArrayList<String>();  
  81.         list.add("xiazdong-1");  
  82.         list.add("xiazdong-2");  
  83.         list.add("xiazdong-3");  
  84.         ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);  
  85.         ListView lv = new ListView(this);  
  86.         lv.setAdapter(adapter);  
  87.         lv.setOnItemClickListener(this);  
  88.         this.setContentView(lv);  
  89.     }  
  90.     @Override  
  91.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
  92.         Toast.makeText(this,list.get(arg2), Toast.LENGTH_SHORT).show();  
  93.     }  
  94. }</pre><br>  
  95. <h3><a name="t24"></a><span style="font-family:'Microsoft YaHei'">(2)自定义适配器BaseAdapter</span></h3>  
  96. </div>  
  97. <div><br>  
  98. </div>  
  99. <br>  
  100. <h1><a name="t25"></a><span style="font-family:'Microsoft YaHei'">二、4种布局介绍</span></h1>  
  101. <div><span style="font-family:'Microsoft YaHei'"><br>  
  102. </span></div>  
  103. <span style="font-family:'Microsoft YaHei'">AbsoluteLayout因为已被废除,因此不做介绍;<br>  
  104. 只要存在界面,就会有布局的存在,就像Swing,虽然一个是桌面应用,一个是手机应用,但是他们都差不多。<br>  
  105. </span>  
  106. <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>  
  107. <br>  
  108. 此处因为布局非常简单,所以就不用代码来讲解了。<br>  
  109. <br>  
  110. <br>  
  111. <h2><a name="t27"></a><span style="font-family:'Microsoft YaHei'">1.LinearLayout</span></h2>  
  112. <span style="font-family:'Microsoft YaHei'"><br>  
  113. <br>  
  114. 默认布局。组件的排列按照预先定义方向很有序的排列,类似于Swing中的FlowLayout;<br>  
  115. 注意点:</span>  
  116. <pre></pre>  
  117. <pre></pre>  
  118. <pre></pre>  
  119. <pre></pre>  
  120. <pre></pre>  
  121. <pre></pre>  
  122. <pre></pre>  
  123. <pre></pre>  
  124. </pre>  
(1)可以在<LinearLayout>中添加android:orientation:vertical/horizontal ;
(2)可以嵌套<LinearLayout>;

2.FrameLayout


每个组件都在左上角,如果多个组件一起出现,则会重叠;

3.RelativeLayout


每个组件定位都是按照与其他组件的上下、左右定位;
默认的定位为左上方;
(1)定位与组件的上下左右
android:layout_below="@id/.."
android:layout_above="@id/"
android:layout_toLeftOf="@id/"
android:layout_toRightOf="@id/"
(2)定位与组件的边缘对齐
android:layout_alignLeft="@id/"
android:layout_alignRight="@id/"
android:layout_alignTop="@id/"
android:layout_alignBottom="@id/"
(3)定位与父组件的边缘对齐
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
(4)与整个屏幕的关系
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"

4.TableLayout


类似于Swing中的GridLayout;
表格布局的每行用<TabRow>括起来;
在<TableLayout>中可以定义如下属性:

(1)android:shrinkColumns="1" 表明第2个控件如果里面的内容过多,会收缩,扩展到第二行,而不是延伸;
(2)android:stretchColumns="2" 如果有空白,第3个控件填充;

在控件中设置:

(1)android:layout_column="2" 将此控件放在第3个位置;
(2)android:layout_span="2" 此控件占据2个单元位置;

补充:


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之间跳转


使用Intent进行多个页面的跳转;
(1)Intent intent = new Intent(Context c,Class class);  c表示当前界面,class表示要跳转到的界面的class;
(2)intent.putExtra(String key,String value);  //设置传输内容;
(3)this.startActivity(intent);   //开始跳转
(4)Intent intent = this.getIntent();  //获得传输来的intent
(5)String value = intent.getStringExtra(String key);   //获得数据

代码示例:


main.xml
[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第一个界面" />  
  11.         <TextView  
  12.         android:id="@+id/tv1"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="" />  
  16.     <EditText   
  17.         android:id="@+id/e1"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:hint="输入信息"  
  21.         />  
  22.     <Button   
  23.         android:id="@+id/b1"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="发送到第二个界面"  
  27.         />  
  28. </LinearLayout>  

mylayout.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="第二个界面" />  
  11.         <TextView  
  12.         android:id="@+id/tv2"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="" />  
  16.     <EditText   
  17.         android:id="@+id/e2"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:hint="输入信息"  
  21.         />  
  22.     <Button   
  23.         android:id="@+id/b2"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="发送到第一个界面"  
  27.         />  
  28. </LinearLayout>  

MultiActivityActivity.java
[java] view plaincopy
  1. package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11.   
  12. public class MultiActivityActivity extends Activity implements OnClickListener{  
  13.     private Button b1;  
  14.     private EditText e1;  
  15.     private TextView tv1;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         b1 = (Button)findViewById(R.id.b1);  
  21.         e1 = (EditText)findViewById(R.id.e1);  
  22.         tv1 = (TextView)findViewById(R.id.tv1);  
  23.         Intent i = this.getIntent();  
  24.         if(i.getStringExtra("2")!=null){  
  25.             tv1.setText(i.getStringExtra("2"));  
  26.         }  
  27.         b1.setOnClickListener(this);  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onClick(View v) {  
  32.           
  33.         Intent intent = new Intent(MultiActivityActivity.this,OtherActivity.class);  
  34.         intent.putExtra("1", e1.getText().toString());  
  35.         this.startActivity(intent);  
  36.     }  
  37. }  

OtherActivity.java

[java] view plaincopy
  1. package org.xiazdong;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.DialogInterface;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.TextView;  
  12.   
  13. public class OtherActivity extends Activity implements OnClickListener{  
  14.     private TextView view ;  
  15.     private Button b2;  
  16.     private EditText e2;  
  17.     private TextView tv2;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         view = new TextView(this);  
  22.         setContentView(R.layout.mylayout);  
  23.         b2 = (Button)findViewById(R.id.b2);  
  24.         e2 = (EditText)findViewById(R.id.e2);  
  25.         tv2 = (TextView)findViewById(R.id.tv2);  
  26.         Intent i = this.getIntent();  
  27.         if(i.getStringExtra("1")!=null){  
  28.             tv2.setText(i.getStringExtra("1"));  
  29.         }  
  30.         b2.setOnClickListener(this);  
  31.     }  
  32.     @Override  
  33.     public void onClick(View v) {  
  34.           
  35.         Intent intent = new Intent(OtherActivity.this,MultiActivityActivity.class);  
  36.         intent.putExtra("2", e2.getText().toString());  
  37.         this.startActivity(intent);  
  38.     }  
  39.       
  40. }  



[html] view plaincopy
  1. <pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
0 0