Android菜鸟在成长-定制对话框和LayoutInflater

本文介绍了Android开发中如何为应用程序定制对话框,详细讲解了使用LayoutInflater来动态加载布局文件的过程,并配以实际效果展示,帮助菜鸟开发者理解并掌握这一核心技术。
摘要由CSDN通过智能技术生成

效果图


main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/mybut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户登录" />
    
</LinearLayout>

login.xml


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:text="用户名:"
            android:textSize="16px" />

        <EditText
            android:layout_height="wrap_content"
            android:width="60pt" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:text="密码:"
            android:textSize="16px" />

        <EditText
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:width="60pt" >

            <requestFocus />
        </EditText>
    </TableRow>

</TableLayout>

package com.example.layoutinflater;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
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 {

	private Button mybut=null;//定义按钮组件
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.mybut=(Button)super.findViewById(R.id.mybut);
		this.mybut.setOnClickListener(new OnClickListenerImpl());
	}
	public class OnClickListenerImpl implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO 自动生成的方法存根
			LayoutInflater factory=LayoutInflater.from(MainActivity.this);
			View myview=factory.inflate(R.layout.login,null);//将布局文件转变为View
			Dialog dialog=new AlertDialog.Builder(MainActivity.this)//创建dialog
			.setTitle("用户登录")
			.setView(myview)
			.setPositiveButton("登陆",//设置确定按钮
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO 自动生成的方法存根
						
					}
				}).setNegativeButton("取消",//设置取消按钮
						new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO 自动生成的方法存根
								
							}
						}).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;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值