UI效果(3): 自定义Dialog

在这篇博客中,你可以了解到下面几项内容:


<1> LayoutInflater 的使用


<2> Dialog、AlertDialog与自定义布局


<3> Button 的onClick属性


运行,界面就是两个BUtton


UI效果(3): 自定义Dialog_第1张图片


自定义Dialog

UI效果(3): 自定义Dialog_第2张图片


自定义AlertDialog

UI效果(3): 自定义Dialog_第3张图片


完整源码下载地址:http://download.csdn.net/source/3512363


1. Activity代码

package mark.zhang;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class CustomDialogActivity extends Activity {
	Builder customAlertDialog = null;
	Dialog customDialog = null;
	
	LayoutInflater inflater = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    }
    
	/**
	 * 为customDialog这个Button设置监听器
	 * 
	 * 注意:在xml文件中设置onClick属性要与改名称一致,否则报错
	 * 
	 * @param v
	 *            该参数是必需的
	 */
    public void customDialog(View v) {
    	customDialog = new Dialog(this); 
	    customDialog.setContentView(R.layout.about);
	    customDialog.setTitle("自定义Dialog");
	    //该句代码如果设置为false的话,点击"返回"按钮不可以退出该dialog
	    //customDialog.setCancelable(false);
	    //View view = inflater.inflate(R.layout.about, (ViewGroup)findViewById(R.id.AboutLinearView));
	    // 自定义布局中的Button
	    final Button btn_ok = (Button) customDialog.findViewById(R.id.about_button);
	    btn_ok.setOnClickListener(new OnClickListener() {// 该事件被触发
			
			@Override
			public void onClick(View v) {
				Log.d("mark", "myself dialog!");
			}
		});
	    // 显示对话框
	    customDialog.show();
    }
    
	/**
	 * 为customAlertDialog这个Button设置监听器
	 * 
	 * 注意:在xml文件中设置onClick属性要与改名称一致,否则报错
	 * 
	 * @param v
	 *            该参数是必需的
	 */
    public void customAlertDialog(View v) {
    	customAlertDialog = new AlertDialog.Builder(this); 
    	// /layout/about.xml自定义布局文件
	    View view = inflater.inflate(R.layout.about, (ViewGroup)findViewById(R.id.AboutLinearView)); 
	    customAlertDialog.setTitle("自定义AlertDialog").setView(view);
	    customAlertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
	    // 自定义布局中的Button
	    final Button btn_ok = (Button) view.findViewById(R.id.about_button);
	    btn_ok.setOnClickListener(new OnClickListener() {// 该事件可以被触发
			
			@Override
			public void onClick(View v) {
				Log.d("mark", "myself alertDialog!");
				// 其它操作,比如结束该Activity或者启动服务、另一个Activity等
			}
		});
	    // 显示对话框
	    customAlertDialog.show();
    }
}
2. main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<Button  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:layout_marginTop="5dip"
	    android:text="customDialog"
	    android:onClick="customDialog"
	    />
	<Button  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:layout_marginTop="10dip"
	    android:text="customAlertDialog"
	    android:onClick="customAlertDialog"
	    />
</LinearLayout>
这里需要注意,两个Button都设置了onClick属性,看看Activity代码中有两个对应的方法。

3. 自定义布局文件about.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:id="@+id/AboutScrollView">
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:id="@+id/AboutLinearView"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		android:orientation="vertical">
		>
		<TextView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/AboutTextView"
			android:textColor="?android:attr/textColorPrimaryDisableOnly"
			android:textAppearance="?android:attr/textAppearanceSmall"
			android:autoLink="all" android:padding="11dp" android:text="@string/about_text" />
		<Button android:id="@+id/about_button" android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:layout_gravity="center_horizontal"
			android:text="ok,I know!" />
	</LinearLayout>

</ScrollView>

你可能感兴趣的:(UI,android,layout,dialog,button,encoding)