TShopping

 找回密碼
 註冊
搜索
查看: 1468|回復: 0
打印 上一主題 下一主題

[轉帖] AlertDialog用法續:在對話框顯示View

[複製鏈接]
跳轉到指定樓層
1#
發表於 2017-11-16 00:18:43 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
有時候AlertDialog上的按鈕,文本並不能滿足我們程序的要求,比如說我們要設計個登錄窗口,如果用上文的方法是沒法插入EditText的,也就無法實現。其實AlertDialog是可以顯示View的,這樣的話,幾乎任何元素都可以在AlertDialog中顯示。
先看效果圖:


下面我們用代碼一步一步實現。
先講思路,首先寫個view的XML文件,把登錄框的提示以用輸入框寫進去,然後把xml文件轉成View類型,再用setView()即可。 下面是我寫好的xml文件,文件名為login.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <TextView android:text="用户名"
  8.           android:layout_width="fill_parent"
  9.           android:layout_height="wrap_content"
  10.           android:layout_marginLeft="10dip"
  11.           android:textSize="25px"
  12.         />
  13. <EditText android:id="@+id/usr"
  14.           android:layout_width="fill_parent"
  15.           android:layout_height="fill_parent"
  16.           android:layout_margin="10dip"
  17. />
  18. <TextView android:text="密码"
  19.           android:layout_width="fill_parent"
  20.           android:layout_height="wrap_content"
  21.           android:layout_marginLeft="10dip"
  22.           android:textSize="25px"
  23.         />
  24. <EditText android:id="@+id/pwd"
  25.           android:layout_width="fill_parent"
  26.           android:layout_height="fill_parent"
  27.           android:layout_margin="10dip"
  28. />
複製代碼


接下來是程序代碼:

  1. package com.pocketdigi.test;

  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.DialogInterface;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;

  10. public class main extends Activity {
  11.     /** Called when the activity is first created. */
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.         Button button=(Button)findViewById(R.id.b1);
  17.         button.setOnClickListener(openDialog);
  18.     }

  19.     private OnClickListener openDialog=new OnClickListener(){
  20.         @Override
  21.         public void onClick(View v) {
  22.             Login();//点击执行弹出登录对话框
  23.         }


  24.     };
  25.     public void Login() {
  26.         LayoutInflater factory=LayoutInflater.from(main.this);
  27.         final View v1=factory.inflate(R.layout.login,null);
  28.                          //R.layout.login与login.xml文件名对应,把login转化成View类型
  29.         AlertDialog.Builder dialog=new AlertDialog.Builder(main.this);
  30.         dialog.setTitle("用户登录");
  31.         dialog.setView(v1);//设置使用View
  32.                           //设置控件应该用v1.findViewById 否则出错
  33.         dialog.setPositiveButton("登录", new DialogInterface.OnClickListener() {
  34.             public void onClick(DialogInterface dialog, int whichButton) {

  35.                 //登录代码
  36.             }
  37.         });
  38.         dialog.setNegativeButton("取消",new DialogInterface.OnClickListener() {
  39.             
  40.             @Override
  41.             public void onClick(DialogInterface dialog, int which) {
  42.                 // TODO Auto-generated method stub
  43.                
  44.             }
  45.         });
  46.         dialog.show();
  47.     }
  48. }
複製代碼

文章出處

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-26 12:33 , Processed in 0.058267 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表