自定义对话框

xml

---------------------------------

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" tools:context=".MainActivity">

<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示登录对话框"
/>
</LinearLayout >

 

login.xml

<?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">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="20dp"
/>
<EditText
android:id="@+id/etName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
android:textSize="20dp"
/>
<EditText
android:id="@+id/etPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
/>
</LinearLayout>


</LinearLayout>

----------------------------------

java

package com.example.huanglifeng.myapp2;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

/**
* Created by huanglifeng on 2015/7/8.
*/
public class ActivityLifeCycle extends Activity implements View.OnClickListener{

@Override
public void onClick(View v)
{
LinearLayout loginLayout=(LinearLayout)getLayoutInflater().inflate(R.layout.login,null);
new AlertDialog.Builder(this)
.setIcon(R.drawable.smile)
.setTitle("用户登录")
.setView(loginLayout)
.setPositiveButton("登录",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//登录
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//取消
}
}).show();

}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}

@Override
protected void onRestart() {
super.onRestart();
}

@Override
protected void onStart() {
super.onStart();
}

@Override
protected void onResume() {
super.onResume();
}

@Override
protected void onPause() {
super.onPause();
}

@Override
protected void onStop() {
super.onStop();
}

@Override
protected void onDestroy() {
super.onDestroy();
}
}

 

你可能感兴趣的:(自定义)