本文共 2368 字,大约阅读时间需要 7 分钟。
创建一个布局文件 my_dialog.xml
下面是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时即弹出自己定义的对话框:
图形界面例如以下:
在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; }}
作者:
版权声明:本文博主原创文章,博客,未经同意不得转载。
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4878292.html,如需转载请自行联系原作者