自定义对话框的制作

1,防开源中国的登陆对话框。效果图如下




2,该对话继承Activity来实现,只有点击返回和关闭图标才能关闭对话框。

3,主要代码,LoginDialog.java 该类为登陆的相关操作,具体的实现更具自己的业务逻辑来操作。

public class LoginDialog extends Activity{


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_dialog);
	}
}

4,点击按钮显示对话框:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	}

	
	//显示对话框
	public void show(View view){
		Intent intent = new Intent(this,LoginDialog.class);
		startActivity(intent);
	}
	
	@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;
	}

}


5,对话框的布局文件
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/logindialog_space" 
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent">
    <ScrollView 
    	android:id="@+id/login_scrollview" 
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content" 
    	android:layout_centerInParent="true"
    	android:paddingTop="14.0dip" 
    	android:paddingLeft="14.0dip" 
    	android:paddingRight="14.0dip" 
    	android:scrollbars="none" >
        <ViewSwitcher 
        	android:id="@+id/logindialog_view_switcher" 
        	android:layout_width="fill_parent" 
        	android:layout_height="wrap_content">
            <RelativeLayout 
            	android:layout_width="fill_parent" 
            	android:layout_height="fill_parent"
            	android:background="@drawable/login_bg" 
            	android:paddingTop="30.0dip"
            	android:paddingBottom="20.0dip" >
                <TableLayout 
	                android:orientation="vertical" 
	                android:id="@+id/login_user_table" 
	                android:layout_width="fill_parent" 
	                android:layout_height="wrap_content" 
	                android:layout_marginTop="10.0dip" 
	                android:layout_marginLeft="15.0dip" 
	                android:layout_marginRight="15.0dip" 
	                android:layout_centerHorizontal="true"
	                android:background="@drawable/login_user_table_bg" >
                    <LinearLayout 
                    	android:id="@+id/login_uly"
                     	android:layout_width="fill_parent" 
                     	android:layout_height="wrap_content">
                        <TextView 
	                         android:layout_width="wrap_content" 
	                         android:layout_height="wrap_content" 
	                         android:layout_marginLeft="8.0dip" 
	                         android:layout_marginTop="10.0dip" 
	                         android:textStyle="bold" 
	                         android:textColor="#ff000000" 
	                         android:text="账号:" />
                        <AutoCompleteTextView 
	                         android:id="@+id/login_account" 
	                         android:layout_width="fill_parent" 
	                         android:layout_height="wrap_content" 
	                         android:layout_marginTop="10.0dip" 
	                         android:layout_marginLeft="8.0dip" 
	                         android:layout_marginRight="8.0dip" 
	                         android:background="@null" 
	                         android:hint="请输入账号" 
	                         android:maxLines="1" 
	                         android:singleLine="true" 
	                         android:selectAllOnFocus="true" 
	                         android:inputType="textEmailAddress" />
                    </LinearLayout>
                    <ImageView 
	                    android:layout_width="fill_parent" 
	                    android:layout_height="wrap_content" 
	                    android:layout_marginTop="5.0dip" 
	                    android:layout_marginLeft="2.0dip" 
	                    android:layout_marginRight="2.0dip" 
	                    android:src="@drawable/line_blue" 
	                    android:scaleType="fitXY" />
                    <LinearLayout 
	                    android:layout_width="fill_parent" 
	                    android:layout_height="wrap_content" 
	                    android:layout_marginTop="7.0dip">
                        <TextView 
	                        android:layout_width="wrap_content" 
	                        android:layout_height="wrap_content" 
	                        android:layout_marginLeft="8.0dip" 
	                        android:textStyle="bold" 
	                        android:textColor="#ff000000" 
	                        android:text="密码:" />
                        <EditText 
	                        android:id="@+id/login_password" 
	                        android:layout_width="fill_parent" 
	                        android:layout_height="wrap_content" 
	                        android:layout_marginLeft="8.0dip" 
	                        android:layout_marginRight="8.0dip" 
	                        android:background="@null" 
	                        android:hint="请输入密码" 
	                        android:maxLines="1" 
	                        android:password="true" 
	                        android:singleLine="true" 
	                        android:selectAllOnFocus="true" />
                    </LinearLayout>
                </TableLayout>
                <TableLayout 
	                android:orientation="vertical" 
	                android:layout_width="fill_parent" 
	                android:layout_height="wrap_content" 
	                android:layout_below="@+id/login_user_table" 
	                android:layout_alignLeft="@+id/login_user_table" 
	                android:layout_alignRight="@+id/login_user_table"
	                android:stretchColumns="0,2" >
                    <TableRow 
	                    android:gravity="center_horizontal"
	                    android:layout_width="wrap_content" 
	                    android:layout_height="wrap_content" 
	                    android:layout_marginTop="5.0dip">
                        <CheckBox 
	                        android:id="@+id/login_checkbox_rememberMe" 
	                        android:layout_width="wrap_content" 
	                        android:layout_height="wrap_content" 
	                        android:layout_gravity="left|center" 
	                        android:paddingLeft="30dip"
	                        android:text="是否记住用户名和密码"
	                        android:checked="true"/>
                    </TableRow>
                    <LinearLayout 
	                    android:layout_width="fill_parent" 
	                    android:layout_height="wrap_content" 
	                    android:layout_marginTop="5.0dip">
                        <Button 
	                        android:id="@+id/login_btn_login" 
	                        android:layout_width="fill_parent" 
	                        android:layout_height="45dip" 
	                        android:text="登陆"
	                      />
                    </LinearLayout>
                </TableLayout>
            </RelativeLayout>
            <View 
	            android:id="@+id/login_loading" 
	            android:layout_width="135.0dip" 
	            android:layout_height="135.0dip"
	            android:layout_gravity="center" 
	            />
        </ViewSwitcher>
    </ScrollView>
    <ImageButton 
	    android:id="@+id/login_close_button" 
	    android:layout_width="wrap_content" 
	    android:layout_height="wrap_content"
	    android:layout_alignRight="@+id/login_scrollview" 
	    android:layout_alignTop="@+id/login_scrollview"
	    android:background="@drawable/login_close_button_nor"  />
</RelativeLayout>

6,在Manifest.xml文件中声明,注意加上主题,否则没有显示效果

 <activity
            android:name="com.qj.foschina.ui.LoginDialog"
            android:theme="@style/Theme.HalfTranslucent" 
            >
        </activity>
7,样式文件
    <style name="Theme.HalfTranslucent" parent="@android:style/Theme.Translucent">
        <item name="android:windowBackground">@color/half_transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

源码下载地址:http://download.csdn.net/detail/lzqjfly/5312162


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
应用程序在运行当中经常要输出各种即时信息,Delphi提供了多种形式的消息对话框可以满足这些要求,尽管可以使用各种标志定制一个消息框,但仍不能将自己的图标放在消息框内,也不能修改其标准行为去创建自己的消息框,其实通过调用函数MessageBoxIndirect()就很容易绕过这些明确的限制,这是一个功能很强、易于使用的函数。 ---- 此函数在WINDOWS单元里面,文件位于Delphi安装路径Source\Rtl\Win里面,需要指出的是,Delphi的原代码中它的返回值类型是BOOL型,而实质上它的返回值应为Integer。修改方法:在WINDOWS单元中找到此函数后,把BOOL改为Integer,共有三处要修改,然后在Delphi安装路径Lib中找到WINDOWS.DCU文件,把它改名或者删除,并把修改后的WINDOWS.PAS源码复制到Lib路径中。 ---- 该函数只有一个参数,类型是TMsgBoxParams,它的结构及说明如下: tagMSGBOXPARAMSA = packed record cbSize: UINT; {数据结构的长度} hwndOwner: HWND; {所有者窗口句柄} hInstance: HINST; {应用程序实例句柄} lpszText: PAnsiChar; {在消息框客户区内显示的文本} lpszCaption: PAnsiChar; {标题栏文本} dwStyle: DWORD; {确定消息框内按钮和图符 的数量及种类的MB_标志} lpszIcon: PAnsiChar; {从资源文件中取出的一个 图符资源的名字} dwContextHelpId: DWORD; {指定帮助文本的ID号} lpfnMsgBoxCallback: TPRMsgBoxCallback; {当用户按下HELP按钮时调用的一个回 调函数} dwLanguageId: DWORD; {显示在按钮内文本的语言定义} end; ---- 由于直接使用此函数比较复杂,我们可以自定义一个函数来封装此函数,对它进行简化,在用法上尽量做到与Delphi的消息框相一致,自定义函数代码如下: function MessageEx (lText,lCaption:PChar; lStyle:DWord;lIcon:PChar):Integer; var Msg:TMsgBoxParams; begin Msg.cbSize:=Sizeof(Msg); Msg.hwndOwner:=Application.Handle; Msg.hInstance:=hinstance; Msg.lpszText:=lText; Msg.lpszCaption:=lCaption; Msg.dwStyle:=lStyle; Msg.lpszIcon:=lIcon; Msg.dwContextHelpId:=1; Msg.lpfnMsgBoxCallback:=nil; Msg.dwLanguageId:=LANG_NEUTRAL; Result:=MessageBoxIndirect(Msg); end; ---- 要想在消息框中显示自己的图标,先准备一个装有图标的资源文件,加在程序中,如{$R c:\mydir\myres.res},在lStyle参数里除了所需的MB_标志外还要加上MB_USERICON标志,并在最后一个参数里写上资源文件中图标的名字,如果不想使用自定义的图标,可将最后一个参数设为nil。函数的其它用法和返回值的处理与Delphi提供的消息框一样。 ---- 强烈建议把自定义函数放在一个单元文件里,并把此文件放在Delphi的搜索路径如Lib下,以后只需要把此单元加在uses语句里,就可以直接使用自定义函数,非常方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值