Android开发--WIFI输入密码Dialog的实现

最终实现效果是点击一个未保存密码的WIFI信号时,弹出如下Dialog:

1、定义Dialog的布局文件:

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>


<?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:gravity="center_horizontal"

android:orientation="vertical"

style="@style/DialogStyle">

<EditText

android:id="@+id/wifiDialogPsw"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:password="true"

android:singleLine="true"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_weight="2">

<Button

android:id="@+id/wifiDialogCancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="取消"

android:layout_weight="1"/>

<Button

android:id="@+id/wifiDialogCertain"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:text="确定"

android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

 来自CODE的代码片

WIFI_Dialog.XML

2、定义一个Dialog的子类

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank id="L39" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<a target=_blank id="L40" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a>
<a target=_blank id="L41" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a>
<a target=_blank id="L42" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;"> 42</a>
<a target=_blank id="L43" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;"> 43</a>
<a target=_blank id="L44" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;"> 44</a>
<a target=_blank id="L45" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;"> 45</a>
<a target=_blank id="L46" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;"> 46</a>
<a target=_blank id="L47" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;"> 47</a>
<a target=_blank id="L48" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;"> 48</a>
<a target=_blank id="L49" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;"> 49</a>
<a target=_blank id="L50" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;"> 50</a>
<a target=_blank id="L51" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;"> 51</a>
<a target=_blank id="L52" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;"> 52</a>
<a target=_blank id="L53" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;"> 53</a>
<a target=_blank id="L54" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;"> 54</a>
<a target=_blank id="L55" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;"> 55</a>
<a target=_blank id="L56" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;"> 56</a>
<a target=_blank id="L57" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L57" rel="#L57" style="color: rgb(102, 102, 102); text-decoration: none;"> 57</a>
<a target=_blank id="L58" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L58" rel="#L58" style="color: rgb(102, 102, 102); text-decoration: none;"> 58</a>
<a target=_blank id="L59" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L59" rel="#L59" style="color: rgb(102, 102, 102); text-decoration: none;"> 59</a>
<a target=_blank id="L60" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L60" rel="#L60" style="color: rgb(102, 102, 102); text-decoration: none;"> 60</a>


package com.wifi.utils;

import android.app.Dialog;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import com.example.controlpcpro.R;

public class WifiPswDialog extends Dialog{

private Button cancelButton;

private Button okButton;

private EditText pswEdit;

private OnCustomDialogListener customDialogListener;

public WifiPswDialog(Context context,OnCustomDialogListener customListener) {

//OnCancelListener cancelListener) {

super(context);

// TODO Auto-generated constructor stub

customDialogListener = customListener;

}

//定义dialog的回调事件

public interface OnCustomDialogListener{

void back(String str);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.wifi_config_dialog);

setTitle("请输入密码");

pswEdit = (EditText)findViewById(R.id.wifiDialogPsw);

cancelButton = (Button)findViewById(R.id.wifiDialogCancel);

okButton = (Button)findViewById(R.id.wifiDialogCertain);

cancelButton.setOnClickListener(buttonDialogListener);

okButton.setOnClickListener(buttonDialogListener);

}

private View.OnClickListener buttonDialogListener = new View.OnClickListener() {

@Override

public void onClick(View view) {

// TODO Auto-generated method stub

if(view.getId() == R.id.wifiDialogCancel){

pswEdit = null;

customDialogListener.back(null);

cancel();//自动调用dismiss();

}

else{

customDialogListener.back(pswEdit.getText().toString());

dismiss();

}

}

};

}

 来自CODE的代码片

WifiPswDialog

首先说明下Dialog中两个Button的点击事件的处理

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>


public void onClick(View view) {

// TODO Auto-generated method stub

if(view.getId() == R.id.wifiDialogCancel){

pswEdit = null;

customDialogListener.back(null);

cancel();//自动调用dismiss();

}

else{

customDialogListener.back(pswEdit.getText().toString());

dismiss();

}

}

 来自CODE的代码片

click

参考:http://labs.chinamobile.com/groups/10670_27134

对话框关闭时,调用dismiss()方法。点击“取消”时调用cancel()方法,指不再需要执行对话框上的任何功能和动作,取消对话框会自动调用dismiss()方法。

实现从对话框中返回信息,如WIFI密码

Dialog并没有提供可以直接返回数据的方法,我们可以通过创建自己的监听类实现

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>


//定义dialog的回调事件

public interface OnCustomDialogListener{

void back(String str);

}

 来自CODE的代码片

OnCustomDialogListener

同样Dialog的构造方法也需要改变

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a>


public WifiPswDialog(Context context,OnCustomDialogListener customListener) {

super(context);

// TODO Auto-generated constructor stub

customDialogListener = customListener;

}

 来自CODE的代码片

WifiPswDialog

然后我们需要在创建Dialog时候实现OnCustomDialogListener接口。

Dialog中如何传出数据呢,这是在OnClick方法中传出的。如代码中的语句:

customDialogListener.back(null);

customDialogListener.back(pswEdit.getText().toString());

3、在Acitivity中创建Dialog

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/liuhui_8989/article/details/22962649#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>


WifiPswDialog pswDialog = new WifiPswDialog(ControlPCMainActivity.this,new OnCustomDialogListener() {

@Override

public void back(String str) {

// TODO Auto-generated method stub

wifiPassword = str;

if(wifiPassword != null){

int netId = localWifiUtils.AddWifiConfig(wifiResultList,wifiItemSSID, wifiPassword);

Log.i("WifiPswDialog",String.valueOf(netId));

if(netId != -1){

localWifiUtils.getConfiguration();//添加了配置信息,要重新得到配置信息

if(localWifiUtils.ConnectWifi(netId)){

selectedItem.setBackgroundResource(R.color.green);

}

}

else{

Toast.makeText(ControlPCMainActivity.this, "网络连接错误", Toast.LENGTH_SHORT).show();

selectedItem.setBackgroundResource(R.color.burlywood);

}

}

else{

selectedItem.setBackgroundResource(R.color.burlywood);

}

}

});

pswDialog.show();

 来自CODE的代码片

snippet_file_0.txt

创建Dialog时最主要的就是对OnCustomDialogListener接口的实现,对back()方法实现,back方法是对话框消失后执行的操作,返回的数据通过back()方法的参数str实现。

创建完成后Dialog的显示只需要通过Dialog.show()方法实现就可以了

project源码:http://download.csdn.net/detail/liuhui_8989/7154671

时间: 2024-11-21 05:44:41

Android开发--WIFI输入密码Dialog的实现的相关文章

wifi开发-android开发 wifi连接不稳定不能上网

问题描述 android开发 wifi连接不稳定不能上网 5C 一个android设备创建热点,另外2个设备去连接这个wifi,一个HTC设备可以连接并上网,另一个设备勉强可以连接(不稳定),但连接上却上网不稳定(有时可以,重启无效).请教大家有遇到过这个问题吗? 解决方案 http://mobile.yesky.com/131/11069131.shtml

Android开发,wifi连接如何知道连接失败是因为密码错误导致

问题描述 Android开发,wifi连接如何知道连接失败是因为密码错误导致 如题,Android开发,wifi连接如何知道连接失败是因为密码错误导致 解决方案 [Android开发]wifi开关与wifi连接(密码连接)iphone5连接wifi显示密码错误

Android开发之缓冲dialog对话框创建、使用与封装操作

本文实例讲述了Android开发之缓冲dialog对话框创建.使用与封装操作.分享给大家供大家参考,具体如下: package com.hstech.handysystem.prompt; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.widget.TextView; import com.hstec

Android开发--WIFI实现

wifi的基本结构 在Android的官方文档中定义了如下五种状态: WIFI_STATE_DISABLING  WIFI网卡正在关闭  0 WIFI_STATE_DISABLED   WIFI网卡不可用  1WIFI_STATE_ENABLING    WIFI网卡正在打开  2WIFI_STATE_ENABLED     WIFI网卡可用  3WIFI_STATE_UNKNOWN    WIFI网卡状态不可知 4 要操作WIFI设备,就需要使用Context.getSystemService

Android开发笔记之:Dialog的使用详解_Android

Dialog是任何系统都必须有的一个控件,作为辅助窗口,用于显示一些消息,或请求用户采取一引起操作等.在Android中也不例外,基本使用可能参看文档.使用时的注意事项1. BACK键能取消掉对话框(dismiss),但是却不会触发其onOkey和onCancel回调接口,所以如果你的对话框会改某些状态,一定要注意还有第三种方式取消对话框.2. 尽量少用模态对话框(Model dialog),如果Dialog.setCancellable(false),就变成了一个模态对话框,除了程序内部把其D

android开发中自定义dialog的布局样式实例

在网上找了很多关于dialog的自定义样式的问题,还有很多人写得比较复杂,需要改动style什么的,或者是自定义dialog搞得很复杂,我最后还是找到了方法来实现. 下面是我的dialog布局xml文件: [mw_shl_code=java,true]<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com

Android 控制wifi 相关操作实例_Android

Android 控制WIFI相关操作 WIFI的全称是Wireless Fidelity,又称802.11b标准,是一种高速的无线通信协议,传输速度可以达到11Mb/s. 实际上,对WIFI并不需要过多的控制(当成功连接WIFI后,就可以直接通过IP在WIFI设备之间进行通信了),一般只需要控制打开或关闭WIFI以及获得一些与WIFI相关的信息(例如,MAC地址.IP等). 如果读者的Android手机有WIFI功能,可以在手机上测试本节的例子.要注意的是,WIFI功能不能在Android模拟器

Android 控制wifi 相关操作实例

Android 控制WIFI相关操作 WIFI的全称是Wireless Fidelity,又称802.11b标准,是一种高速的无线通信协议,传输速度可以达到11Mb/s. 实际上,对WIFI并不需要过多的控制(当成功连接WIFI后,就可以直接通过IP在WIFI设备之间进行通信了),一般只需要控制打开或关闭WIFI以及获得一些与WIFI相关的信息(例如,MAC地址.IP等). 如果读者的Android手机有WIFI功能,可以在手机上测试本节的例子.要注意的是,WIFI功能不能在Android模拟器

【Android】Android中WIFI开发总结(一)

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://smallwoniu.blog.51cto.com/3911954/1334951        WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为"热点".      在Android中对Wifi操作,android本身提供了