[Android] AlertDialog with Edittext

Android 建立含 EditText 輸入框的 AlertDialog 程式碼範例,帶 Ok/Cancel 按鈕,可接收使用者輸入的文字。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
  AlertDialog.Builder alert = new AlertDialog.Builder(this);  
	  
	alert.setTitle("Title");  
	alert.setMessage("Message");  
	// Set an EditText view to get user input   
	final EditText input = new EditText(this);  
	alert.setView(input);  
	  
	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
	 public void onClick(DialogInterface dialog, int whichButton) {  
	 String value = input.getText();  
	 Do something with value!  
	 }  
	});  
	  
	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
	 public void onClick(DialogInterface dialog, int whichButton) {  
	 // Canceled.  
	 }  
	});  
	  
	alert.show();  

就這樣XD

ref: http://www.androidsnippets.com/prompt-user-input-with-an-alertdialog

comments powered by Disqus
Powered by Hugo. Theme Stack. All Rights Reserved.