首页 > 代码库 > android 自定义对话框
android 自定义对话框
新建一个布局文件 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">用户名</string> <string name="textView2">密码</string> </resources>
在主activity_main.xml中添加一个按钮控件,当单击此按钮时即弹出自定义的对话框:
<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>图形界面如下:
在MainActivity.java代码中为按钮控件设置单击事件监听,并弹出自定义的对话框:
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
android 自定义对话框
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。