android开发之AlertDialog

几个常用的方法:

1.setTitle()

设置对话框显示的标题。

2.setIcon()

设置对话框的图标,值得注意的是,如果没有使用setTitle()方法,setIcon()不会生效。

3.setMessage()

设置对话框显示内容。

4.setPositiveButton()、setNegativeButton()、setNeutralButton()

设置对话框的按钮。

5.setCancelable()

点击对话框以外,对话框不会消失,按返回键对话框也不会消失。


提示对话框

技术分享

演示实例:

MainActivity.java

public class MainActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	public void click(View v) {

		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setMessage("这是一个提示框");
		builder.setIcon(R.drawable.icon);
		builder.setTitle("提示");
		builder.setPositiveButton("Positive",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1) {

					}
				});

		builder.setNegativeButton("Negative", new OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub

			}
		});

		builder.setNeutralButton("Neutral", new OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub

			}
		});
		builder.show();
	}
}

activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
    
    <Button 
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"
        android:onClick="click"/>


</LinearLayout>

列表对话框

技术分享

演示实例:

MainActivity.java

public class MainActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	public void click(View v) {

		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		//builder.setMessage("我是提示对话框"); 设置setMessage后,setItems失效
		builder.setIcon(R.drawable.icon);
		builder.setTitle("提示");
		builder.setItems(new String[] { "one", "two", "three" },
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int index) {
						String str = null;
						switch (index) {
						case 0:
							str = "one";
							break;
						case 1:
							str = "two";
							break;
						case 2:
							str = "three";
							break;
						}
						Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();

					}
				});
		builder.show();
	}
}

activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
    
    <Button 
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"
        android:onClick="click"/>


</LinearLayout>

单选列表对话框

技术分享

演示实例:

MainActivity.java

public class MainActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	public void click(View v) {

		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setIcon(R.drawable.icon);
		builder.setTitle("提示");

		builder.setSingleChoiceItems(new String[] { "one", "two", "three" }, 0,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int index) {

						String str = null;
						switch (index) {
						case 0:
							str = "one";
							break;

						case 1:
							str = "two";
							break;

						case 2:
							str = "three";
							break;

						}
						Toast.makeText(MainActivity.this, str,
								Toast.LENGTH_SHORT).show();

					}
				});
		builder.setPositiveButton("确认", null);
		builder.show();
	}
}
activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
    
    <Button 
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"
        android:onClick="click"/>


</LinearLayout>

复选列表对话框

技术分享

演示实例:

MainActivity.java

public class MainActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	public void click(View v) {

		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setIcon(R.drawable.icon);
		builder.setTitle("提示");

		final boolean[] items = new boolean[] { false, false, false };
		builder.setMultiChoiceItems(new String[] { "one", "two", "three" },
				items, new OnMultiChoiceClickListener() {

					@Override
					public void onClick(DialogInterface arg0, int arg1,
							boolean isCheaked) {

					}
				});
		builder.setPositiveButton("确认", null);
		builder.show();
	}
}
activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
    
    <Button 
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"
        android:onClick="click"/>


</LinearLayout>
 


自定义对话框

技术分享

MainActivity.java

public class MainActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	public void click(View v) {

		AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
		builder.setTitle("提示");
		builder.setIcon(R.drawable.icon);
		
		ImageView imageView = new ImageView(MainActivity.this);
		imageView.setImageResource(R.drawable.close);
		//获取布局文件
		View myView = this.getLayoutInflater().inflate(R.layout.form, null);
		builder.setView(myView);

		ListView listView = (ListView) myView.findViewById(R.id.lv);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				MainActivity.this, android.R.layout.simple_list_item_1,
				new String[] { "1", "2", "3" });
		listView.setAdapter(adapter);
		
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int positon, long arg3) {
				Toast.makeText(MainActivity.this,""+ positon, Toast.LENGTH_SHORT)
						.show();
			}
		});

		builder.setPositiveButton("确认", null);
		builder.show();
	}
}

activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}">
    
    
    <Button 
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"
        android:onClick="click"/>
    
    

</LinearLayout>
form.xml(对话框显示的布局内容)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView 
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></ListView>
    

</LinearLayout>


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。