您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在Android中利用Dialog實現一個對話框功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
一、普通對話框
AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("溫馨提示");//標題 builder.setMessage("天氣冷,注意保暖"); builder.setIcon(R.mipmap.ic_launcher); builder.create(); builder.show();
普通對話框
二、確定取消對話框
builder.setTitle("確定取消對話框"); builder.setMessage("請選擇確定或取消"); builder.setIcon(R.mipmap.ic_launcher); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { //正能量按鈕 Positive @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你點擊了確定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你選擇了取消", Toast.LENGTH_SHORT).show(); } }); builder.create().show();
確定取消對話框
三、多按鈕對話框
builder.setTitle("多個按鈕對話框"); builder.setMessage("請選擇"); builder.setIcon(R.mipmap.ic_launcher); builder.setPositiveButton("我沒玩夠", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "繼續瀏覽精彩內容", Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("開啟", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "起床了", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("我累了,要休息一下", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "歡迎再來", Toast.LENGTH_SHORT).show(); } }); builder.create().show();
多按鈕對話框
四、列表對話框
final String arrItem[] = getResources().getStringArray(R.array.aikaifa); builder.setItems(arrItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你選擇了第" + arrItem[which], Toast.LENGTH_SHORT).show(); } }); builder.create().show();
列表對話框
五、帶Adapter的對話框
builder.setTitle("帶Adapter的對話框"); builder.setIcon(R.mipmap.ic_launcher); final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); int arrImg[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; for (int i = 0; i < arrImg.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("img", arrImg[i]); map.put("title", "愛開發" + i); list.add(map); } SimpleAdapter adapter = new SimpleAdapter(activity, list, R.layout.list_item, new String[]{"img", "title"}, new int[]{R.id.iv, R.id.tv}); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, "你選擇了" + list.get(which).get("title").toString().trim(), Toast.LENGTH_SHORT).show(); } }); builder.create().show();
帶Adapter的對話框
六、單選對話框
builder.setTitle("單選對話框"); builder.setIcon(R.mipmap.ic_launcher); builder.setSingleChoiceItems(R.array.aikaifa, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(activity, which+"", Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show();
單選對話框
七、多選對話框
builder.setTitle("多選對話框"); builder.setIcon(R.mipmap.ic_launcher); builder.setMultiChoiceItems(R.array.aikaifa, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(activity, which+""+isChecked, Toast.LENGTH_SHORT).show(); } }); builder.create().show();
多選對話框
八、日期對話框
DatePickerDialog datePickerDialog=new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(activity, year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show(); } }, 2017, 02, 9); datePickerDialog.show();
日期對話框
九、時間對話框
TimePickerDialog timePickerDialog=new TimePickerDialog(activity, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(activity, hourOfDay+"小時"+minute+"分鐘", Toast.LENGTH_SHORT).show(); } }, 17, 49, true); timePickerDialog.show();
時間對話框
十、自定義對話框
View view= LayoutInflater.from(activity).inflate(R.layout.dialog_login, null); builder.setView(view); builder.create(); final EditText et_phone=(EditText)view.findViewById(R.id.et_phone); final EditText et_password=(EditText)view.findViewById(R.id.et_password); Button btn_submit=(Button)view.findViewById(R.id.btn_submit); btn_submit.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(activity, "手機號碼:"+et_phone.getText().toString()+" 短信驗證碼:"+et_password.getText().toString(), Toast.LENGTH_SHORT).show(); } }); builder.show();
自定義對話框
項目設計到的xml
list_item.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:background="#f5f5f5" android:orientation="horizontal" android:padding="10dp"> <ImageView android:id="@+id/iv" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:text="標題" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
dialog_login.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="8dp" android:orientation="vertical" android:padding="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:text="驗證手機號碼" android:textColor="#414141" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:hint="請輸入手機號碼" android:inputType="number" android:maxLength="11" android:paddingLeft="10dp" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginTop="10dp"> <EditText android:id="@+id/et_password" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="4" android:gravity="center_vertical" android:hint="請輸入短信驗證碼" android:inputType="number" android:maxLength="6" android:paddingLeft="10dp" android:textSize="14sp" /> <TextView android:id="@+id/tv_get_code" android:layout_width="0dp" android:layout_height="48dp" android:layout_marginLeft="10dp" android:layout_weight="2" android:enabled="false" android:gravity="center" android:text="點擊獲取" android:textColor="#000000" /> </LinearLayout> <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="提交" /> </LinearLayout> </RelativeLayout>
看完上述內容,你們對如何在Android中利用Dialog實現一個對話框功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。