一.Dialog(對話方塊):
我們在android開發當中總會用到一些對話方塊,用來與使用者進行互動。
需要通過AlertDialog.Builder類,來實現一個對話方塊的建立。
常用的方法:
setTille() : 給對話方塊設定title;
setIcon() : 給對話方塊設定表徵圖
setMessage() : 設定對話方塊的提示資訊。
setItems() : 設定對話方塊要顯示的一個list,一般用於顯示幾個命令時。
setSingleChoiceItems() : 設定對話方塊顯示一個單選的List。
setMultiChoiceItems() : 用來設定對話方塊顯示一系列的複選框。
setPositiveButton() : 給對話方塊添加"yes"按鈕。
setNegativeButton() : 給對話方塊添加"No"按鈕。
Dialog布局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.31" android:text="使用者名稱:" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.31" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.31" android:text="密碼:" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.31" android:password="true" android:ems="10" /> </LinearLayout></LinearLayout>
主介面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
主代碼:
public class MainActivity extends Activity {private TextView text;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);text = (TextView)findViewById(R.id.text);AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("登陸提示") //設定標題.setMessage("請登陸") //設定內容.setPositiveButton("確定", //設定確定按鈕new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) { //點擊"確定"轉向登陸框LayoutInflater factory = LayoutInflater.from(MainActivity.this);//得到自訂登陸框View dv = factory.inflate(R.layout.activity_main, null);AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).setTitle("登陸框").setView(dv) // 設定自訂對話方塊的樣式.setPositiveButton("確定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubtext.setText("你已經登陸成功");}} ).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubtext.setText("你已經登陸失敗");}}).create();dlg.show();}}).setNegativeButton("退出", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubMainActivity.this.finish();}}).create();dialog.show();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
運行介面:
點擊確定之後:
·
二.ProgressBar(進度條):
android系統提供了兩大類進度條樣式,長條進度條(progress-BarStyleHorizontal)和圓形進度條(progressBarStyleLarge),進度條可以再進行應用程式裝載資源時應用,可以提示使用者稍等等等。
布局:
<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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請點擊按鈕" /> <ProgressBar android:id="@+id/p1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <ProgressBar android:id="@+id/p2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="100" style="?android:attr/progressBarStyleLarge" android:progress="50" android:secondaryProgress="70" android:visibility="gone" /> <Button android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始" /> </LinearLayout>
Id為p1的是長形進度條,Id為p2的是圓形進度條,style設定進度條的風格。
style="?android:attr/progressBarStyleHorizontal" 長形進度條
style="?android:attr/progressBarStyleLarge" 超大號圓形ProgressBar
style="?android:attr/progressBarStyleSmall" 小號圓形ProgressBar
style="?android:attr/progressBarStyleSmallTitle" 標題型圓形ProgressBar
主代碼:
public class MainActivity extends Activity {private ProgressBar p1 ;private ProgressBar p2 ;private Button b1;protected static final int STOP_NOTIFIER = 0x108;protected static final int THREADING_NOTIFIER = 0x109;public int counter = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//設定視窗模式requestWindowFeature(Window.FEATURE_PROGRESS);setProgressBarVisibility(true);setContentView(R.layout.activity_main); p1 = (ProgressBar)findViewById(R.id.p1);p2 = (ProgressBar)findViewById(R.id.p2);b1 = (Button)findViewById(R.id.b1);b1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//設定ProgressBar為可見狀態p1.setVisibility(View.VISIBLE);p2.setVisibility(View.VISIBLE);//設定ProgressBar的最大值p1.setMax(100);//設定ProgressBar當前值p1.setProgress(0);p2.setProgress(0);//通過線程來改變ProgressBar的值new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor(int i = 0;i<10;i++){try{counter =(i+1)*20;Thread.sleep(1000);if(i == 4){Message m = new Message();m.what = MainActivity.STOP_NOTIFIER;MainActivity.this.handler.sendMessage(m);break;}else{Message m = new Message();m.what = MainActivity.THREADING_NOTIFIER;MainActivity.this.handler.sendMessage(m);}}catch(Exception e){e.printStackTrace();}}}}).start();}});} // 更新介面顯示Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stub switch(msg.what){//ProgressBar已經是最大值case MainActivity.STOP_NOTIFIER:p1.setVisibility(View.GONE);p2.setVisibility(View.GONE);//中斷線程Thread.currentThread().interrupt();break;case THREADING_NOTIFIER:if(!Thread.currentThread().isInterrupted()){p1.setProgress(counter);p2.setProgress(counter);//設定標題列中前景的一個進度條進度值setProgress(counter*100);//設定標題列中後面的一個進度條進度值setSecondaryProgress(counter*100);} break;}super.handleMessage(msg);}};@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
運行圖片: