创建一个布局文件 my_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="4dp"
android:text="@string/textView1"
/> <EditText
android:id="@+id/editText_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="4dp"
/> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText_user"
android:layout_below="@+id/editText_user"
android:layout_marginTop="7dp"
android:text="@string/textView2"
/> <EditText
android:id="@+id/editText_pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_marginTop="4dp"
android:layout_below="@+id/textView2"
/>
</RelativeLayout>
下面是strings.xml文件:
<? xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">demo091801</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> <string name="MyDialog">自己定义对话框</string>
<string name="textView1">username</string> <string name="textView2">password</string> </resources>
在主activity_main.xml中加入一个button控件,当单击此button时即弹出自己定义的对话框:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/My_Dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/MyDialog"
/>
</RelativeLayout>
图形界面例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHAzMTAwMTg5MzE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
在MainActivity.java代码中为button控件设置单击事件监听,并弹出自己定义的对话框:
MainActivity.java:
package com.example.demo091801; import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn1 = (Button)findViewById(R.id.My_Dialog); btn1.setOnClickListener(new OnClickListener()
{ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.my_dialog, null); builder.setTitle("登陆");
builder.setView(view);
builder.setPositiveButton("登陆", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub }
}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub }
}); AlertDialog dialog = builder.create();
dialog.show(); }
}
); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
作者:http://blog.csdn.net/lp310018931
版权声明:本文博主原创文章,博客,未经同意不得转载。