本博文源于安卓基礎對話框的實作。先講理論後看實踐。大家先可以看實踐的效果,看到不懂的代碼才回頭看理論,加深理論的學習。知行合一方能做到知識有的放矢。
理論篇–Dialog類
對話框是提示使用者做出決定或輸入額外資訊的小視窗,對話框不會填充螢幕。對話框是一個有邊框和标題欄的、獨立存在的容器,再應用程式中經常使用對話框元件來進行人機互動,用于需要使用者采取行動才能繼續執行的事件。
Android系統提供了豐富的對話框功能。Dialog是所有對話框的基類,AlertDialog是Dialog的直接派生類。
消息對話框
消息對話框(AlertDialog)是應用程式設計中最常用的對話框之一。AlertDialog的内容很豐富,可以放置标題、消息和3個按鈕。使用它可以建立普通對話框、帶清單的對話框,以及帶單選按鈕和複選框的對話框。AlertDialog的常用方法如表
方法 | 說明 |
---|---|
AlertDialog.Builder(Context) | 對話框Builder對象的構造方法 |
create() | 建立AlertDialog對象 |
setTitle() | 設定對話框标題 |
setIcon() | 設定對話框圖示 |
setMessage() | 設定對話框的消息 |
setItems() | 設定對話框要顯示的一個清單 |
setPositiveButton() | 在對話框中添加肯定按鈕 |
setNegativeButton() | 在對話框中添加否定按鈕 |
show() | 顯示對話框 |
dismiss() | 關閉對話框 |
建立AlertDialog對象需要使用AlertDialog的内部類Builder,設計AlertDialog的步驟如下。
(1)用AlertDialog.Builder類建立對話框Builder對象
(2)設定對話框的标題、圖示、提示資訊内容、按鈕等
dialog.setTitle("普通對話框")
dialog.setIcom(R.drawable.icon1);
dialog.setMessage("一個簡單的提示對話框");
dialog.setPositiveButton("确定",new okClick());
(3)建立并顯示AlertDialog對話框對象。
dialog.create();
dialog.show();
其他常用對話框
AlertDialog對話框是最重要的對話框,有多種表現形式、包括清單對話框、單選對話框、多選對話框、進度條對話框(ProgressDialog)、時間對話框(DatePickerDialog和TimePickerDIalog)、自定義布局對話框等形式。其中時間對話框帶有允許使用者選擇日期或時間的預定義界面。
進度條對話框
PressDialog類繼承于AlertDialog,綜合了進度條與對話框的特點,使用起來非常簡單。ProgressDialog的常用方法如表所示:
方法 | 說明 |
---|---|
getMax() | 擷取對話框進度的最大值 |
getProgress() | 擷取對話框目前進度值 |
onStart() | 開始調用對話框 |
setMax(int max) | 設定對話框進度的最大值 |
setMessage(CharSequence message) | 設定對話框的消息 |
setProgress(int vlaue) | 設定對話框目前進度 |
show(COntext context,CharSequence title,CharSequence message) | 設定對話框的标題和消息 |
ProgressDialog(Context context) | 對話框的構造方法 |
應用篇–實作使用者登入對話框
測試效果
點選普通對話框,彈出最平常的對話框,點選輸入對話框,可以輸入對話框,其中對話框的内容可以自由輸入。在這裡密碼隻要為admin就可以登入,否則就是登陸失敗。
制作步驟
建立新項目 My servenApp
點進Project—>Empty Activity—>然後名字改下,finish即可。成功之後,點選箭頭運作程式。
程式正常可以跑成功hello world字樣,下面我們繼續
上傳資源到drawable
将此圖另存為“xx.gif"
拖到drawable,ok後,我們基礎圖檔有了
布局主界面activity_main.xml
這個是主界面搭建的源碼,下面部分會對他細緻講解
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id = "@+id/button1"
android:text="打開普通對話框"
android:textSize="24sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id = "@+id/button2"
android:text="打開輸入對話框"
android:textSize="24sp"/>
</LinearLayout>
LinearLayout詳解
線性布局作為根元素,設定width與height,方向為垂直。
Button詳解
兩個按鈕作為響應事件width與height。id一定要設定,為了變得更加友好。text也要設定,textSize隻是為了更美觀。
建立空類 搭建次要界面
次要界面就是實作對話框的彈出。這也就是使用者登入對話框,後續java代碼會對此進行相應事件配置。
右擊app檔案夾。箭頭操作!将其命名為DialogActivity
布局對話框界面的xml
也就是次界面的布局,主要實作使用者登入對話框的界面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用者名:"
android:id="@+id/user"
android:textSize="18sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼:"
android:id="@+id/textView"
android:textSize="18sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/paswdEdit"/>
</LinearLayout>
LinearLayout詳解
簡單的height與width設定,方向設定為垂直
TextView詳解
也就是我們看到的使用者名和密碼這兩個顯示内容,不可編輯是以TextView。
EditText詳解
id一定要設定,友善後面操作!
編輯主界面java代碼
也就是兩個按鈕的代碼,使用者點選進行相應。
package com.example.myapplication;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class DialogActivity extends Activity implements View.OnClickListener {
Button btn1,btn2;
LinearLayout login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
public void onClick(View arg0) {
AlertDialog.Builder dialog = new AlertDialog.Builder(DialogActivity.this);
if(arg0==btn1)
{
dialog.setTitle("警告");
dialog.setIcon(R.drawable.xx);
dialog.setMessage("本項操作可能導緻資訊洩露!");
dialog.setPositiveButton("确定",new okClick());
dialog.create();
dialog.show();
}
else if(arg0==btn2)
{
login = (LinearLayout)getLayoutInflater().inflate(R.layout.login,null);
dialog.setTitle("使用者登入").setMessage("請輸入使用者名和密碼").setView(login);
dialog.setPositiveButton("确定",new loginClick());
dialog.setIcon(R.drawable.xx);
dialog.create();
dialog.show();
}
}
class okClick implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog,int which)
{
dialog.cancel();
}
}
class loginClick implements DialogInterface.OnClickListener{
EditText txt;
@Override
public void onClick(DialogInterface dialog,int which)
{
txt = (EditText)login.findViewById(R.id.paswdEdit);
if((txt.getText().toString()).equals("admin"))
Toast.makeText(getApplicationContext(),"登入成功",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),"密碼錯誤",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
class exitClick implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog,int which)
{
DialogActivity.this.finish();
}
}
}
主界面代碼看起來非常複雜,其實本身内容非常靈清。分為兩種。第一種激活控件,将id一一get,這是第一種。第二種就是對使用者所點選地進行相應事件的編輯。主要采用java的複寫思想。然後在第二種使用者登入對話框需要建立和顯示對話框。我們還做了單擊事件也要注意!
點選運作
程式運作點選綠色按鈕
測試效果
總結
代碼得以運作主要有以下步驟
- 建立項目,跑成功hello world
- 上傳資源檔案到drawable
- 布局主界面兩個按鈕
- 建立空類作為對話框
- 布局對話框xml檔案
- 配置主界面事件相應的代碼
- 成功運作,收獲喜悅!
很高興博文能幫助到大家!一些檔案名的修改也要思考的。如果大家檔案名有問題,可以參考部落客做這個實驗的目錄,僅供參考!
內建的mainfest預設是MainActivity.java要考慮到喲!