91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Android中利用Dialog實現一個對話框功能

發布時間:2020-12-03 17:37:49 來源:億速云 閱讀:168 作者:Leah 欄目:移動開發

今天就跟大家聊聊有關如何在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

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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實現一個對話框功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

迁安市| 鄄城县| 抚远县| 塔城市| 贵南县| 奎屯市| 翁源县| 大港区| 福泉市| 邯郸市| 清丰县| 仁布县| 黄浦区| 平南县| 囊谦县| 陵川县| 淮安市| 平顶山市| 曲水县| 高碑店市| 五河县| 耒阳市| 阳山县| 思茅市| 西林县| 景德镇市| 利辛县| 普兰店市| 通榆县| 绍兴市| 乌恰县| 忻城县| 视频| 榆林市| 虞城县| 卓资县| 高陵县| 商水县| 工布江达县| 南丰县| 太白县|