android的弹出框 -(二)超级进阶篇--自定义弹出框

来源:互联网 发布:滤波器计算软件 编辑:程序博客网 时间:2024/06/08 03:24

昨天写了一篇博客,关于普通的弹出框的,本来想把自定义的弹出框也一起发出来的,但想想时间不早了,岛主还是去看球吧啊哈哈。。





是不是觉得很神奇,这里我们将自定义的弹出框设置了逻辑来判断两次密码是不是相同的
闲话不说我们来分析一个

这里是继承我上一篇来写的,所以上面还有几个按键,不过大同小异。http://blog.csdn.net/qq_33599978/article/details/53648718

1.首先我们是不是点击了一个按钮跳出弹出框  这里我们设置一个Button的android:onClick="Bt4"方法就行就行

这里谷歌的工程师们帮我实现了onClickd的方法,这里只有实现这个我们定义的方法Bt4就好

这里是在xml里设置这个  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.m_6dialog.MainActivity" >       <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="Bt4"        android:text="自定义弹出框" /></LinearLayout>

2. 其实我们在弹出那个验证密码的是不是也算一个界面尼,所以我们还需要在layout里面创建这个界面来好实现点击弹出的功能  这里myview就是我们的验证密码界面

 

<?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:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="设置密码" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="30dp"        android:text="请输入密码:" />    <EditText        android:id="@+id/pwd1"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="请在次输入密码:" />    <EditText        android:id="@+id/pwd2"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:orientation="horizontal" >    <Button        android:id="@+id/btcen"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:layout_marginRight="51dp"        android:text="取消" />    <Button        android:id="@+id/btsure"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="53dp"        android:text="确定" /></RelativeLayout></LinearLayout>

3.现在是不是要去MainActivity.java 中实现尼  哈哈对的

这里不懂的可以看我上一篇博客为什么方法不是放在onCreat 方法。http://blog.csdn.net/qq_33599978/article/details/53648718

这里我们需要注意是为什么下面四个找ID是要在findViewById前面加载View尼,
想想,这个是不是默认在activity_main.xml里面找,但是我们的不是在其他,xml .中嘛,所以需要加上

final EditText editText = (EditText) view.findViewById(R.id.pwd1);
final EditText editText2 = (EditText) view.findViewById(R.id.pwd2);
Button button = (Button) view.findViewById(R.id.btsure);
Button button2 = (Button) view.findViewById(R.id.btcen);

package com.example.m_6dialong;import java.util.Date;import java.util.zip.Inflater;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.app.AlertDialog.Builder;import android.app.DatePickerDialog;import android.app.DatePickerDialog.OnDateSetListener;import android.app.Dialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.os.Bundle;import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.DatePicker;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void Bt4(View view) {zidingy();}private void zidingy() {/* * 1.AlertDialog 弹出提示框  * 2.哪一个view来接我们定义的提示框  * 3.final 找到两个密码,和找到两个按钮  * 4.final AlertDialog alertDialog = builder.create(); * alertDialog.setView(view); alertDialog.show(); 来使用  * 5.写个点击事件并且判断事件是否两次密码相同 //import android.view.View.OnClickListener * 两个只能存在一个 // import android.content.DialogInterface.OnClickListener; * 6. */AlertDialog.Builder builder = new AlertDialog.Builder(this);View view = View.inflate(this, R.layout.myview, null);final AlertDialog alertDialog = builder.create();alertDialog.setView(view);alertDialog.show();final EditText editText = (EditText) view.findViewById(R.id.pwd1);final EditText editText2 = (EditText) view.findViewById(R.id.pwd2);Button button = (Button) view.findViewById(R.id.btsure);Button button2 = (Button) view.findViewById(R.id.btcen);button.setOnClickListener(new View.OnClickListener() {String pwd1 = "";String pwd2 = "";String msg = "";@Overridepublic void onClick(View v) {// TODO Auto-generated method stubpwd1 = editText.getText().toString();pwd2 = editText2.getText().toString();if (check()) {Toast.makeText(MainActivity.this, "我是输了什么,哇好厉害呀",Toast.LENGTH_SHORT).show();alertDialog.dismiss();} else if (!msg.equals("")) {Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();msg = "";} else {Toast.makeText(MainActivity.this, "第二次我输出是什么啊,是德玛西亚",Toast.LENGTH_SHORT).show();}}private boolean check() {if ("".equals(pwd2) && "".equals(pwd1)) {msg = "sure 菜鸟们 ,怎么忘记了女朋友生日";return false;} else {return pwd1.equals(pwd2);}}});button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubalertDialog.dismiss();}});}}

4. 我们实现逻辑代码部分的思想

这里我们需要获取两次密码,然后在中去配对是不是相同密码,然后你知道的在做个判断就好Toast一下就好了,不懂得就有恶补一下JAVa了哈
private boolean check() {if ("".equals(pwd2) && "".equals(pwd1)) {msg = "sure 菜鸟们 ,怎么忘记了女朋友生日";return false;} else {return pwd1.equals(pwd2);}}

5.如何获取两次密码

pwd1 = editText.getText().toString();pwd2 = editText2.getText().toString();
两段代码就是获取,然后强制转String类型。

到底完结

2 0
原创粉丝点击