步骤:

1.定义自定义的Dialog的布局文件

2.写一个类MyDialog继承Dialog

3.Dialog 返回值到Activity的方法是定义一个接口,接口中定义返回值到Activity的方法,MyDialog实现这个接口,然后在需要的位置调用接口中的方法,Activity实现这个接口,

重写接口的方法

代码如下

return_code_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="@color/white"
android:weightSum="1"> <TextView
android:id="@+id/title"
android:layout_width="321dp"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="24dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/black"
/>
<TextView
android:id="@+id/content"
android:layout_marginTop="10dp"
android:layout_width="305dp"
android:layout_gravity="center"
android:textColor="@color/viewfinder_mask"
android:layout_height="wrap_content"
android:textSize="18dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="20dp"
android:password="true"
android:layout_height="50dp"
android:background="@drawable/bg_edit_normal"
android:id="@+id/psw_1"
android:digits="1234567890"/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:background="@drawable/bg_edit_normal"
android:id="@+id/psw_2"
android:password="true"
android:gravity="center"
android:textSize="20dp"
android:digits="1234567890"
/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:background="@drawable/bg_edit_normal"
android:id="@+id/psw_3"
android:password="true"
android:gravity="center"
android:textSize="20dp"
android:digits="1234567890"
/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:background="@drawable/bg_edit_normal"
android:id="@+id/psw_4"
android:password="true"
android:gravity="center"
android:textSize="20dp"
android:digits="1234567890"
/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:background="@drawable/bg_edit_normal"
android:id="@+id/psw_5"
android:password="true"
android:gravity="center"
android:textSize="20dp"
android:digits="1234567890"
/>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:background="@drawable/bg_edit_normal"
android:id="@+id/psw_6"
android:password="true"
android:gravity="center"
android:textSize="20dp"
android:digits="1234567890"
/>
</LinearLayout>
</LinearLayout>
interface MyJudgePasswordInterface
public interface MyJudgePasswordInterface {
public void returnJudgePasswordResult(boolean result);
}

PasswordDialog.java

package com.example.administrator.bigelephantbike;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView; import org.w3c.dom.Text; /**
* Created by Administrator on 2016/4/3.
*/
public class PasswordDialog extends Dialog implements TextWatcher,View.OnKeyListener{ private Context context;
private String title;
private String content;
private TextView titleTextView;
private TextView contentTextVIew;
private EditText eidtText1;
private EditText eidtText2;
private EditText eidtText3;
private EditText eidtText4;
private EditText eidtText5;
private EditText eidtText6;
private String password;
private int current=0;
private String[]passwordInput =new String[6];
private int maxLen =1;//限制EditText的输入长度
private MyJudgePasswordInterface myJudgePasswordInterface; public PasswordDialog(Context context) {
super(context);
}
public PasswordDialog(Context context, int themeResId) {
super(context, themeResId);
}
public PasswordDialog(Context context,String title,String content,String password,MyJudgePasswordInterface myJudgePasswordInterface){
super(context,R.style.add_dialog);
this.context =context;
this.title=title;
this.content =content;
this.password =password;
this.myJudgePasswordInterface =myJudgePasswordInterface;
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
void init(){
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.return_code_dialog, null);
setContentView(view); titleTextView =(TextView)findViewById(R.id.title);
contentTextVIew=(TextView)findViewById(R.id.content);
eidtText1 =(EditText)findViewById(R.id.psw_1);
eidtText2 =(EditText)findViewById(R.id.psw_2);
eidtText3 =(EditText)findViewById(R.id.psw_3);
eidtText4 =(EditText)findViewById(R.id.psw_4);
eidtText5 =(EditText)findViewById(R.id.psw_5);
eidtText6 =(EditText)findViewById(R.id.psw_6); titleTextView.setText(title);
contentTextVIew.setText(content); //锁定数字键盘
eidtText1.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
eidtText2.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
eidtText3.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
eidtText4.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
eidtText5.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
eidtText6.setInputType(EditorInfo.TYPE_CLASS_NUMBER); //添加内容改变的事件监听
eidtText1.addTextChangedListener(this);
eidtText2.addTextChangedListener(this);
eidtText3.addTextChangedListener(this);
eidtText4.addTextChangedListener(this);
eidtText5.addTextChangedListener(this);
eidtText6.addTextChangedListener(this); eidtText1.setOnKeyListener(this);
eidtText2.setOnKeyListener(this);
eidtText3.setOnKeyListener(this);
eidtText4.setOnKeyListener(this);
eidtText4.setOnKeyListener(this);
eidtText5.setOnKeyListener(this);
eidtText6.setOnKeyListener(this); } @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = 0;
//Log.d("test","current="+current+"我变了");
switch (current) {
case 0:
length = eidtText1.getText().length();
changeFocus(length, eidtText2);
passwordInput[0] = eidtText1.getText().toString();
break;
case 1:
length = eidtText2.getText().length();
changeFocus(length, eidtText3);
passwordInput[1] = eidtText2.getText().toString();
break;
case 2:
length = eidtText3.getText().length();
changeFocus(length, eidtText4);
passwordInput[2] = eidtText3.getText().toString();
break;
case 3:
length = eidtText4.getText().length();
changeFocus(length, eidtText5);
passwordInput[3] = eidtText4.getText().toString();
break;
case 4:
length = eidtText5.getText().length();
changeFocus(length, eidtText6);
passwordInput[4] = eidtText5.getText().toString();
break;
case 5:
length = eidtText6.getText().length();
if (length >= 1) {
passwordInput[5] = eidtText6.getText().toString();
myJudgePasswordInterface.returnJudgePasswordResult(validatePassword());
this.cancel();
}
}
}
@Override
public void afterTextChanged(Editable s) { }
void changeFocus(int l,EditText editText){
if(l >= maxLen){
current++;
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
editText.findFocus();
}
}
boolean validatePassword(){
current=0;
StringBuffer stringBuffer=new StringBuffer();
for(int i=0;i<passwordInput.length;i++){
stringBuffer.append(passwordInput[i]);
}
String psw =stringBuffer.toString();
return psw.equals(password);
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL
&& event.getAction() == KeyEvent.ACTION_DOWN) {
current--;
switch(current){
case 0:eidtText1.setText("");requestF(eidtText1);break;
case 1:eidtText2.setText("");requestF(eidtText2);break;
case 2:eidtText3.setText("");requestF(eidtText3);break;
case 3:eidtText4.setText("");requestF(eidtText4);break;
case 4:eidtText5.setText("");requestF(eidtText5);break;
case 5:eidtText6.setText("");requestF(eidtText6);break;
}
}
return false;
}
void requestF(EditText editText){
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
editText.findFocus();
}
}

ChargeActivity.java

package com.example.administrator.bigelephantbike;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import wheelview.ArrayWheelAdapter;
import wheelview.OnWheelChangedListener;
import wheelview.WheelView; public class ChargeActivity extends Activity implements OnWheelChangedListener,View.OnClickListener{
private Button recoverBtn;
private String passwordReturn="123456";
private String passwordRecover="234567"; private PasswordDialog myDialog;
//密码输入是否正确
private boolean pswResult;
//实现MyJudgePasswordInterface
private MyJudgePasswordInterface myJudgePasswordInterface;
//1标识输入的是还车密码,2表示输入的是恢复密码
private int flag=0; private String []nums={"0","1","2","3","4","5","6","7","8","9"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_charge); returnBtn =(Button)findViewById(R.id.return_btn);
recoverBtn =(Button)findViewById(R.id.recover_btn); myJudgePasswordInterface=new MyJudgePasswordInterface() {
@Override
public void returnJudgePasswordResult(boolean result) {
pswResult = result;
}
}; returnBtn.setOnClickListener(this);
recoverBtn.setOnClickListener(this); }
@Override
public void onClick(View v) {
//当点击还车按钮时
if(v.getId()==R.id.return_btn){
flag=1;
myDialog =new PasswordDialog(ChargeActivity.this,this.getResources().getString(R.string.input_return_code),this.getResources().getString(R.string.return_tips),passwordReturn,myJudgePasswordInterface);
myDialog.show();
}
if(v.getId()==R.id.recover_btn){
flag=2;
myDialog=new PasswordDialog(ChargeActivity.this,this.getResources().getString(R.string.input_recover_tips),this.getResources().getString(R.string.recover_tips),passwordRecover,myJudgePasswordInterface);
myDialog.show();
}
}
}
												

自定义Dialog以及Dialog返回值到Activity的更多相关文章

  1. linux shell自定义函数(定义、返回值、变量作用域)介绍

    http://www.jb51.net/article/33899.htm linux shell自定义函数(定义.返回值.变量作用域)介绍 linux shell 可以用户定义函数,然后在shell ...

  2. android 16 带返回值的activity

    main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...

  3. 05 SpringMVC:02.参数绑定及自定义类型转换&&04.SpringMVC返回值类型及响应数据类型&&05.文件上传&&06.异常处理及拦截器

    springMVC共三天 第一天: 01.SpringMVC概述及入门案例 02.参数绑定及自定义类型转换 03.SpringMVC常用注解 第二天: 04.SpringMVC返回值类型及响应数据类型 ...

  4. linux shell 自定义函数(定义、返回值、变量作用域)介绍

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...

  5. 转 linux shell自定义函数(定义、返回值、变量作用域)介绍

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...

  6. linux shell学习笔记二---自定义函数(定义、返回值、变量作用域)介绍

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用.下面说说它的定义方法,以及调用需要注意那些事项. 一.定义shell函数(define function) 语法: [ f ...

  7. activity之间參数传递&amp;&amp;获取activity返回值&amp;&amp;activity生命周期

    Activity之间參数传递 A activity想将參数传给B activity时能够利用Intent将消息带过去 Intent intent = new Intent(this,BActivity ...

  8. Android - 和其他APP交互 - 获得activity的返回值

    启用另一个activity不一定是单向的.也可以启用另一个activity并且获得返回值.要获得返回值的话,调用startActivityForResult()(而不是startActivity()) ...

  9. Intent获取Activity返回值

    /* Intent获取Activity返回值* 三步:* 子Activity关闭后的返回值处理函数,requestCode是子Activity返回的请求码,与页面顶端的两个请求码相匹配,resultC ...

随机推荐

  1. Mybatis对MySQL中BLOB字段的读取

    1.在sqlMapConfig中,定义一个typeHandlers <typeHandlers> <typeHandler jdbcType="BLOB" jav ...

  2. codeforces C. Bits(数学题+或运算)

    题意:给定一个区间,求区间中的一个数,这个数表示成二进制的时候,数字1的个数最多! 如果有多个这样的数字,输出最小的那个! 思路:对左区间的这个数lx的二进制 从右往左将0变成1,直到lx的值大于右区 ...

  3. [IR] Index Construction

    Three steps to construct Inverted Index as following: 最难的step中: Token sequence. Sort by term. Dictio ...

  4. Parallax.js – 自适应智能设备方向的视差效果插件

    Parallax.js 是一个简单的,轻量级的的视差引擎,能够对智能设备的方向作出反应.在没有没有陀螺仪或运动检测硬件可用的时候,使用光标的位置来代替.有很多的行为,你就可以设置为任何给定的视差实例. ...

  5. sublime 插件zen coding

    sublime的插件Zen Coding是一个编写html的神器,现在已经更名为Emmet了. 在sublime中的package需要搜索的是Emmet 相关网站: 官网 Zen Coding: 一种 ...

  6. C# 文字转声音

    添加COM组件引用:Microsoft Speech object library private SpVoice voice; private void button1_Click(object s ...

  7. Swift可空(Optional)类型基础

    可空类型,对于熟悉C#的同学一定不会陌生.在C#里面值类型都是不能为空的,比如int类型默认为0,bool默认为false.但是我们给int加上?后,就是一个可空类型了. 那么Swift里面呢.Swi ...

  8. URL与图像格式

    绝对/相对URL “绝对URL”是指资源的完整的地址,通常以“http://”打头: “相对URL”是指Internet上资源相对于当前页面的地址,它包含从当前页面指向目标资源位置的路径,不以“htt ...

  9. 使用innerHTML获取HTML代码时,HTML标记属性的双引号好多都消失不见了,原来是属性值中包含空格才会保留双引号

    最近搞的一个项目中所使用的方式比较奇怪,用Label显示HTML内容,然后不断地使用JS把Label的innerHTML复制到TextBox中. 但是,昨天发现了一个问题,获取元素值的时候,有时候正常 ...

  10. DataTable 获取列名 DataTable批量更新至数据库

    好久没写东西了,这几个月也没下功夫钻研技术,愧疚啊.说下最近刚学会的DataTable 的用法吧,新手适合看下. 1 DataTable 获取列名 在处理数据的时候大家都会用到模型,从datatabl ...