继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

设置密码进入界面SettingPwdDemo

慕妹3146593
关注TA
已关注
手记 107
粉丝 14
获赞 39

 

密码设置,可更改密码,输入密码之后才能进入界面,例如密码正确之后才能进入查看个人信息
先看截图,界面有点儿小丑,自己根据需要在修改啦~~  

         5badf8f700013cbd07130799.jpg                                  

开始就是进入这个界面的,设置初始密码。

5badf908000127f807140714.jpg

是设置密码成功后,第二次进入的界面

这个是更改密码输入错误提示

5badf9180001359406800670.jpg

上代码啦~

[代码]java代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

package com.rachel.settingpwddemo;

 

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.lang.reflect.Field;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.DialogInterface.OnKeyListener;

import android.text.method.NumberKeyListener;

import android.util.Log;

import android.view.KeyEvent;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

        String   fileName = "syspass.txt";

        public boolean   fileIsExists(){

            try{

                File   f = new File(fileName);

                if(f.exists()==true){

                    return   false;

                }else{

                    return   true;

                }

            }catch(Exception   e){

                return   false;

            }

        }

        String   syspassword = null;

         

         

        @Override

        protected   void onCreate(Bundle savedInstanceState) {

            //   TODO Auto-generated method stub

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

             

            inputTitleDialog();

             

        }

 

 

         private   void inputTitleDialog() {//获取初始密码,判断是否正确

                if(!fileIsExists()==false){

                        FileInputStream   is = null;

                        ByteArrayOutputStream   os2 = null;

 

                        try{

                            is   = openFileInput(fileName);

                            os2   = new ByteArrayOutputStream();

                            int   len = 0;

                            byte[]   buffer = new byte[1024];

                            while((len=is.read(buffer))!=-1){

                                os2.write(buffer,0,len);

                            }

                        }catch(IOException   e){

                            Log.i("SettingsActivity","error");

                        }finally{

                            try{

                                if(os2!=null){

                                    os2.close();

                                }

                                if(is!=null){

                                    is.close();

                                }

                            }catch(IOException   e){

                                Log.i("SettingsActivity","error");

                            }

                        }

 

                        if(os2!=null){

                            byte[]   content_byte = os2.toByteArray();

                            syspassword   = new String(content_byte);

                        }

                }else{

                    syspassword=null;

                }

                final   String syspass = syspassword;

                if(syspass==null){

                    showSetDialog();

                }else{

                    final   EditText passEdit = new EditText(this);

 

                    numericOnlyListener(passEdit);

 

                     

                    AlertDialog   builder = new AlertDialog.Builder(this)

                    .setTitle("键入密码")

                    .setIcon(R.drawable.ic_launcher)

                    .setView(passEdit)

                    .setOnKeyListener(new   OnKeyListener(){

                        public   boolean onKey(DialogInterface dialog,int keyCode,KeyEvent event){

                            if(keyCode   == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){

                                MainActivity.this.finish();

                                return   false;

                            }

                            return   false;

                        }

                    })

                    .setNegativeButton(getString(R.string.preference_change_password_title),//change   the password

                            new   DialogInterface.OnClickListener() {

                                public   void onClick(DialogInterface dialog, int which) {

                                        String   inputPassWord = passEdit.getText().toString();

                                        if(!(inputPassWord.equals(syspass))   || inputPassWord==null){

                                            Toast.makeText(MainActivity.this,"旧密码错误,请重试",Toast.LENGTH_SHORT).show();

                                            //   SettingsActivity.this.finish();

                                            passEdit.setText("");

                                            try{

                                                //The   following three sentences control the closing of the frame

                                                //这三句代码是设置dialog是否关闭

                                                Field   field=dialog.getClass().getSuperclass().getDeclaredField("mShowing");

                                                field.setAccessible(true);

                                                field.set(dialog,false);//true:Close   the dialog box  false:The dialog box is not closed

                                            }catch(Exception   e){

                                                e.printStackTrace();

                                            }

                                             

                                        }else{

                                            showSetDialog();

                                        }

                                }

                    })

                    .setPositiveButton(getString(R.string.lockpassword_ok_label),

                            new   DialogInterface.OnClickListener() {

                                public   void onClick(DialogInterface dialog, int which) {

 

                                        String   inputPassWord = passEdit.getText().toString();

                                        if(!(inputPassWord.equals(syspass))   ){

                                            Toast.makeText(MainActivity.this,R.string.lockpassword_confirm_passwords_dont_match,Toast.LENGTH_SHORT).show();

                                            MainActivity.this.finish();

                                        }

                                }

                    })

                    .show();

                    builder.setCanceledOnTouchOutside(false);

                }

            }

 

            public   void showSetDialog() {  //设置初始密码

                    LayoutInflater   factory = LayoutInflater.from(this); 

                    final   View textEntryView = factory.inflate(R.layout.dialog, null); 

                    final   EditText editTextPass = (EditText)   textEntryView.findViewById(R.id.editTextPass); 

                    final   EditText editTextrePass =   (EditText)textEntryView.findViewById(R.id.editTextrePass);

 

                    numericOnlyListener(editTextrePass);

                    numericOnlyListener(editTextPass);

 

                    AlertDialog   ad1 = new AlertDialog.Builder(this)

                    .setTitle("设置密码:")

                    .setIcon(R.drawable.ic_launcher)  

                    .setView(textEntryView)

                    .setOnKeyListener(new   OnKeyListener(){

                        public   boolean onKey(DialogInterface dialog,int keyCode,KeyEvent event){

                            if(keyCode   == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){

                                MainActivity.this.finish();

                                return   false;

                            }

                            return   false;

                        }

                    })

                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {   

                        public   void onClick(DialogInterface dialog, int i) {  

                            //   Prompt two passwords do not matchdestv

                             

                            if(!(editTextPass.getText().toString().equals(editTextrePass.getText().toString()))   || editTextPass.getText().toString().equals("") ||   editTextrePass.getText().toString().equals("")){//

                                Toast.makeText(MainActivity.this,R.string.lockpassword_confirm_passwords_dont_match,Toast.LENGTH_LONG).show();

                                MainActivity.this.finish();

 

                            }else{

 

                                FileOutputStream   os = null;

                                try{

                                    os   = openFileOutput(fileName,Context.MODE_PRIVATE);

                                    os.write((editTextPass.getText().toString()).getBytes());

                                }catch(IOException   e){

                                    Log.i("MainActivity","error");

                                }finally{

                                    try{

                                        os.close();

                                    }catch(IOException   e){

                                        Log.i("MainActivity","error");

                                    }

                                }

                            }

                        }   

                    })    

                    .show();

                    ad1.setCanceledOnTouchOutside(false);

            }

             

            /**

             *   设置输入框输入密码的类型

             *   这里只能输入小写字母和数字,输入以外的都无效

             *   可以根据自己的需要设置

             *   @param v

             */

            public   void numericOnlyListener(EditText v){

                v.setKeyListener(new   NumberKeyListener() {

                     

                    @Override

                    public   int getInputType() {

                        //   TODO Auto-generated method stub

                        return   android.text.InputType.TYPE_CLASS_TEXT;

                    }

 

                    @Override

                    protected   char[] getAcceptedChars() {

                        //   TODO Auto-generated method stub

                        char   numberChars[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'};

                                return   numberChars;

                    }

                });

            }

    }

xml布局文件activity_main.xml 我没有添加什么东西,这个根据自己的需求输入密码正确后进入的界面
还有个dialog的布局文件

dialog.xml

[代码]java代码:

?

1

2

3

<relativelayout 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" tools:context=".MainActivity">      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br>      xmlns:tools="http://schemas.android.com/tools"<br>      android:layout_width="match_parent"<br>      android:layout_height="match_parent"<br>      tools:context=".MainActivity" ><br><br>      <EditText<br>          android:id="@+id/editTextPass"<br>          android:layout_width="fill_parent"<br>          android:layout_height="wrap_content"<br>          android:hint="密码" /><br><br>      <EditText<br>          android:id="@+id/editTextrePass"<br>          android:layout_width="fill_parent"<br>          android:layout_height="wrap_content"<br>          android:layout_below="@+id/editTextPass"<br>          android:ems="10"<br>          android:hint="确认密码" /><br><br></RelativeLayout><edittext   android:id="@+id/editTextPass" android:layout_width="fill_parent"   android:layout_height="wrap_content" android:hint="密码">

   <edittext   android:id="@+id/editTextrePass" android:layout_width="fill_parent"   android:layout_height="wrap_content" android:layout_below="@+id/editTextPass"   android:ems="10" android:hint="确认密码">

</edittext></edittext></relativelayout>


strings.xml

[代码]java代码:

?

1

<string   name="preference_change_password_title">更改密码</string><br>      <string name="lockpassword_ok_label">确定</string><br>    <string name="lockpassword_confirm_passwords_dont_match">旧密码不匹配</string><string   name="lockpassword_confirm_passwords_dont_match"></string>

代码就这些,很简单。需要的话可以直接添加到项目中去。

原文链接:http://www.apkbus.com/blog-683250-62008.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP