Android自定义登陆窗口-对话框
2012-12-12 23:12
423 查看
dilog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/txt_loginerror" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:textColor="#ff0000" android:text="输入的账号和密码不正确" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" android:visibility="invisible" /> <TextView android:id="@+id/username" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="账号" android:gravity="left" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/txt_username" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/password" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:textAppearance="?android:attr/textAppearanceMedium" android:text="密码" android:gravity="left" /> <EditText android:id="@+id/txt_password" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:autoText="false" android:capitalize="none" android:gravity="fill_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/txt_toregister" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:textColor="#2200C1" android:textAppearance="?android:attr/textAppearanceMedium" android:text="没有账号?快速注册" android:gravity="left" /> </LinearLayout>
代码里面:
private void CreateLoginAlert() { LayoutInflater factory = LayoutInflater.from(LoginActivity.this); //得到自定义对话框 View DialogView = factory.inflate(R.layout.login_dialog, null); AlertDialog.Builder ad =new AlertDialog.Builder(this); ad.setTitle("账号登陆"); ad.setView(DialogView); adi= ad.create(); adi.setButton("登录", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub username = (EditText)adi.findViewById(R.id.txt_username); password = (EditText)adi.findViewById(R.id.txt_password); loginerror = (TextView)adi.findViewById(R.id.txt_loginerror); m_Dilog=ProgressDialog.show(LoginActivity.this, "请等待...", "正在为你登陆...",true); mRedrawHandler.sleep(100); } }); adi.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); adi.show(); } private RefreshHandler mRedrawHandler = new RefreshHandler(); class RefreshHandler extends Handler{ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub try { socket = new Socket("113.250.155.194", 9999); in = new BufferedReader(new InputStreamReader( socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } if(username.getText().length() == 0) { adi.show(); loginerror.setText("账号不能为空!"); loginerror.setVisibility(loginerror.VISIBLE); }else if(password.getText().length() == 0){ adi.show(); loginerror.setText("密码不能为空!"); loginerror.setVisibility(loginerror.VISIBLE); }else{ UserModel users = new UserModel(); users.setUserName(username.getText().toString()); users.setUserPass(password.getText().toString()); users.setUserState(1); users.setGuanliyuan(0); out.println("login&" + UserModel.userToString(users)); out.flush(); String line; try { line = in.readLine(); System.out.println("登录窗口从服务器收到的消息为:" + line); if (line.startsWith("LOGINOK")) { System.out.println(line.toString()); UserModel user = UserModel.getUserByString(line); out.println("updateList&" + UserModel.userToString(user)); out.flush(); Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); //ChatFrame cf = new ChatFrame(user.getNick(), user.getGuanliyuan()); // cf.setLocation(250, 50); //cf.setVisible(true); //cf.connect(in, out); //this.dispose(); // return; }else if (line.equals("ERROR")) { Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show(); adi.show(); adi.findViewById(R.id.txt_loginerror).setVisibility(View.VISIBLE); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ m_Dilog.dismiss(); } super.handleMessage(msg); } } public void sleep(long delayMillis) { this.removeMessages(0); sendMessageDelayed(obtainMessage(0), delayMillis); } }
就这么简单,效果图如下:
相关文章推荐
- Android简单的自定义用户登陆对话框
- android 自定义窗口大小在java中设定
- Android 自定义对话框(仿iPhoneQQ)
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- Android控件之AlertDialog-自定义对话框
- Android对话框(简单对话框,等待对话框,自定义对话框)
- Android 登陆对话框
- Android 自定义dialog样式位置大小,判断是否第一次登陆
- android 自定义对话框 背景透明
- android中的对话框:自定义对话框
- Android---功能模块一之颜色选择器自定义Preference 对话框
- android开发步步为营之25:开发自定义进度条对话框
- Android对话框自定义标题 对话框标题美化操作
- Android自定义等待对话框
- Android深度自定义Dialog对话框
- Android实现自定义对话框
- android,Alertdialog,自定义对话框
- Android自定义对话框
- Android 自定义对话框,可设置大小和显示位置,并设置点击其他位置不消失
- 我的Android进阶之旅------>Android自定义窗口标题实例