Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開發之AlertDialog

android開發之AlertDialog

編輯:關於Android編程

幾個常用的方法:

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

 

 


列表對話框

 

\

演示實例:

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

 

 


單選列表對話框

 

\

演示實例:

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

 

 


復選列表對話框

 

\

演示實例:

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

 

 

 

 

自定義對話框

\
 

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 adapter = new ArrayAdapter(
				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();
	}
}
 

 




    
    



 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved