系统定义弹窗以及自定义弹窗

知识点:

1 系统定义弹窗

2 自定义弹窗


1 系统定义弹窗

android 中系统弹窗可以方便的进行弹窗提醒功能:

使用AlertDialog.Builder Dialog = new AlertDialog.Builder(this);

设置相关属性之后,show()出Dialog即可。

系统定义弹窗以及自定义弹窗_第1张图片

代码:

/**
	 * 展示更新对话框
	 */
	protected void ShowDialog() {
		AlertDialog.Builder Dialog = new AlertDialog.Builder(this);

		Dialog.setTitle("检查更新");
		Dialog.setMessage(mDescription);

		Dialog.setPositiveButton("立即更新", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				DownLoadAPK();
			}
		});
		Dialog.setNegativeButton("以后再说", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				EnterHomeActivity();
			}
		});

		Dialog.setOnCancelListener(new OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				
				EnterHomeActivity();
			}
		});
		Dialog.show();
	}


2 自定义弹窗:

1 需要编写弹窗xml文件

2 将弹窗xml文件放入到dialog中

系统定义弹窗以及自定义弹窗_第2张图片          系统定义弹窗以及自定义弹窗_第3张图片

step 1:弹窗的xml文件:

<?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:background="#fff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_dialog_set_passwrod_titile"
        android:layout_width="match_parent"
        android:layout_height="30sp"
        android:background="@android:color/holo_orange_light"
        android:gravity="center"
        android:textSize="22sp"
        android:text="手机防盗" />

    <EditText
        android:id="@+id/et_dialog_set_passwrod_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        android:hint="请输入密码"
        android:singleLine="true"
         >

    </EditText>

    <EditText
        android:id="@+id/et_dialog_set_passwrod_comformpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       
        android:hint="请输入确认密码"
        android:singleLine="true"
         />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button_dialog_set_passwrod_OK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="确认" />

        <Button
            android:id="@+id/button_dialog_set_passwrod_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消" />

    </LinearLayout>

</LinearLayout>


step2 :将弹窗布局xml文件放入到弹窗中

1 获得dialog,注意在create的时候,进行了类型转换

AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
			final AlertDialog dialog = builder.create();

2 将布局放入

	View view=View.inflate(HomeActivity.this, R.layout.dialog_set_password, null);		
	dialog.setView(view, 0, 0, 0, 0);
setview(view,0,0,0,0)是为了兼容低版本的androd系统

完整代码:

protected void ShowInputPasswordDialog() {
			AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
			final AlertDialog dialog = builder.create();
				
			View view=View.inflate(HomeActivity.this, R.layout.dialog_set_password, null);		
			
			final EditText ed_password = (EditText)view.findViewById(R.id.et_dialog_set_passwrod_password);
			final EditText ed_comformpassword = (EditText)view.findViewById(R.id.et_dialog_set_passwrod_comformpassword);
			
			
			Button Btn_ok = (Button)view.findViewById(R.id.button_dialog_set_passwrod_OK);
			Button Btn_cancel=(Button)view.findViewById(R.id.button_dialog_set_passwrod_cancel);			
			
			dialog.setView(view, 0, 0, 0, 0);
		
			sharedPreferences = getSharedPreferences("config", MODE_PRIVATE);
			
			Btn_ok.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
			
					//获取要放到点击事件内部,在外部会丢掉数据
					password =ed_password.getText().toString();
					comformpassword=ed_comformpassword.getText().toString();
					

					if(!TextUtils.isEmpty(password))
					{
						
						System.out.println("password : "+password+" comformpassword :" +comformpassword);
						if(password.equals(comformpassword))
						{
							sharedPreferences.edit().putString("password", password).commit();
							Toast.makeText(HomeActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
							dialog.dismiss();
							
						}
						else{
							
							Toast.makeText(HomeActivity.this, "登陆失败,请输入两个相同的密码", Toast.LENGTH_SHORT).show();
						}
					}			
					
				}
			});
			
			Btn_cancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
				
					dialog.dismiss();
				}
			});
			
			dialog.show();
	   
		}



你可能感兴趣的:(android,项目)