Android 登录对话框 实现

该博客介绍了如何在Android应用中创建一个自定义登录对话框。通过展示XML布局文件和Activity代码,展示了如何利用AlertDialog构建包含账号和密码输入框的对话框,并实现了点击按钮后的登录逻辑,包括显示进度对话框并在模拟的登录过程中使用线程处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout 
  3.   xmlns:android="http://schemas.android.com/apk/res/android" 
  4.   android:orientation="vertical" 
  5.   android:layout_width="fill_parent" 
  6.   android:layout_height="fill_parent"> 
  7. <TextView  
  8.     android:id="@+id/AccountTextView" 
  9.     android:layout_width="wrap_content" 
  10.     android:layout_height="wrap_content" 
  11.     android:layout_marginLeft="20dip" 
  12.     android:text="账号" 
  13.     android:gravity="left" 
  14. /> 
  15. <EditText  
  16.     android:id="@+id/AccountEditText" 
  17.     android:layout_width="fill_parent" 
  18.     android:layout_height="fill_parent" 
  19.     android:layout_marginLeft="20dip" 
  20.     android:layout_marginRight="20dip" 
  21.     android:scrollHorizontally="true" 
  22.     android:autoText="false" 
  23.     android:capitalize="none" 
  24.     android:gravity="fill_horizontal" 
  25. /> 
  26. <TextView  
  27.     android:id="@+id/PasswordTextView" 
  28.     android:layout_width="wrap_content" 
  29.     android:layout_height="wrap_content" 
  30.     android:layout_marginLeft="20dip" 
  31.     android:text="密码" 
  32.     android:gravity="left" 
  33. /> 
  34. <EditText  
  35.     android:id="@+id/PasswordEidtText" 
  36.     android:layout_width="fill_parent" 
  37.     android:layout_height="fill_parent" 
  38.     android:layout_marginLeft="20dip" 
  39.     android:layout_marginRight="20dip" 
  40.     android:scrollHorizontally="true" 
  41.     android:autoText="false" 
  42.     android:capitalize="none" 
  43.     android:gravity="fill_horizontal" 
  44.     android:password="true" 
  45. /> 
  46. </LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<TextView 
	android:id="@+id/AccountTextView"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_marginLeft="20dip"
	android:text="账号"
	android:gravity="left"
/>
<EditText 
	android:id="@+id/AccountEditText"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:layout_marginLeft="20dip"
	android:layout_marginRight="20dip"
	android:scrollHorizontally="true"
	android:autoText="false"
	android:capitalize="none"
	android:gravity="fill_horizontal"
/>
<TextView 
	android:id="@+id/PasswordTextView"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_marginLeft="20dip"
	android:text="密码"
	android:gravity="left"
/>
<EditText 
	android:id="@+id/PasswordEidtText"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:layout_marginLeft="20dip"
	android:layout_marginRight="20dip"
	android:scrollHorizontally="true"
	android:autoText="false"
	android:capitalize="none"
	android:gravity="fill_horizontal"
	android:password="true"
/>
</LinearLayout>


Activity

  1. package com.zeph.android; 
  2.  
  3. import android.app.Activity; 
  4. import android.app.AlertDialog; 
  5. import android.app.ProgressDialog; 
  6. import android.content.DialogInterface; 
  7. import android.os.Bundle; 
  8. import android.view.LayoutInflater; 
  9. import android.view.View; 
  10.  
  11. public class Android_DialogActivity extends Activity { 
  12.     /** Called when the activity is first created. */ 
  13.     ProgressDialog p_dialog; 
  14.  
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) { 
  17.         super.onCreate(savedInstanceState); 
  18.         setContentView(R.layout.main); 
  19.         AlertDialog dialog = new AlertDialog.Builder( 
  20.                 Android_DialogActivity.this
  21.                 .setTitle("登录提示"
  22.                 .setMessage("是否登录"
  23.                 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 
  24.  
  25.                     @Override 
  26.                     public void onClick(DialogInterface dialog, int which) { 
  27.                         // TODO Auto-generated method stub 
  28.                         LayoutInflater factory = LayoutInflater 
  29.                                 .from(Android_DialogActivity.this); 
  30.                         final View DialogView = factory.inflate( 
  31.                                 R.layout.dialog, null); 
  32.                         AlertDialog dlg = new AlertDialog.Builder( 
  33.                                 Android_DialogActivity.this
  34.                                 .setTitle("登陆框"
  35.                                 .setView(DialogView) 
  36.                                 .setPositiveButton("确定"
  37.                                         new DialogInterface.OnClickListener() { 
  38.  
  39.                                             @Override 
  40.                                             public void onClick( 
  41.                                                     DialogInterface dialog, 
  42.                                                     int which) { 
  43.                                                 // TODO Auto-generated method 
  44.                                                 // stub 
  45.                                                 p_dialog = ProgressDialog 
  46.                                                         .show(Android_DialogActivity.this
  47.                                                                 "请等待"
  48.                                                                 "正在为您登录..."
  49.                                                                 true); 
  50.                                                 new Thread() { 
  51.                                                     public void run() { 
  52.                                                         try
  53.                                                             sleep(3000); 
  54.                                                         } catch (Exception e) { 
  55.                                                             e.printStackTrace(); 
  56.                                                         } finally
  57.                                                             p_dialog.dismiss(); 
  58.                                                         } 
  59.                                                     } 
  60.                                                 }.start(); 
  61.                                             } 
  62.                                         }) 
  63.                                 .setNegativeButton("取消"
  64.                                         new DialogInterface.OnClickListener() { 
  65.  
  66.                                             @Override 
  67.                                             public void onClick( 
  68.                                                     DialogInterface dialog, 
  69.                                                     int which) { 
  70.                                                 // TODO Auto-generated method 
  71.                                                 // stub 
  72.                                                 Android_DialogActivity.this 
  73.                                                         .finish(); 
  74.                                             } 
  75.                                         }).create(); 
  76.                         dlg.show(); 
  77.  
  78.                     } 
  79.                 }) 
  80.                 .setNegativeButton("退出", new DialogInterface.OnClickListener() { 
  81.  
  82.                     @Override 
  83.                     public void onClick(DialogInterface dialog, int which) { 
  84.                         // TODO Auto-generated method stub 
  85.                         Android_DialogActivity.this.finish(); 
  86.                     } 
  87.                 }).create(); 
  88.         dialog.show(); 
  89.     } 
package com.zeph.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class Android_DialogActivity extends Activity {
	/** Called when the activity is first created. */
	ProgressDialog p_dialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		AlertDialog dialog = new AlertDialog.Builder(
				Android_DialogActivity.this)
				.setTitle("登录提示")
				.setMessage("是否登录")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						LayoutInflater factory = LayoutInflater
								.from(Android_DialogActivity.this);
						final View DialogView = factory.inflate(
								R.layout.dialog, null);
						AlertDialog dlg = new AlertDialog.Builder(
								Android_DialogActivity.this)
								.setTitle("登陆框")
								.setView(DialogView)
								.setPositiveButton("确定",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												// TODO Auto-generated method
												// stub
												p_dialog = ProgressDialog
														.show(Android_DialogActivity.this,
																"请等待",
																"正在为您登录...",
																true);
												new Thread() {
													public void run() {
														try {
															sleep(3000);
														} catch (Exception e) {
															e.printStackTrace();
														} finally {
															p_dialog.dismiss();
														}
													}
												}.start();
											}
										})
								.setNegativeButton("取消",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												// TODO Auto-generated method
												// stub
												Android_DialogActivity.this
														.finish();
											}
										}).create();
						dlg.show();

					}
				})
				.setNegativeButton("退出", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Android_DialogActivity.this.finish();
					}
				}).create();
		dialog.show();
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值