Tuesday, June 26, 2012

If you want to create a pop-window, it is you feel is to think AlertDialog 
But the Android AlertDialog very powerful 
It can not just put text, you can also put any of the components, 



First of all, we put in the xml above three button 
<?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="wrap_content" android:layout_height="wrap_content" android:id="@+id/alertdialog1" android:text="AlertDialog1" > </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/alertdialog2" android:text="AlertDialog2" > </Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/alertdialog3" android:text="AlertDialog3" > </Button> </LinearLayout> 



Then in the onCreate above to initialize them 
 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getButtonView(); setButtonEvent(); } public void getButtonView(){ alertDialog1 = (Button)findViewById(R.id.alertdialog1); alertDialog2 = (Button)findViewById(R.id.alertdialog2); alertDialog3 = (Button)findViewById(R.id.alertdialog3); } public void setButtonEvent(){ alertDialog1.setOnClickListener(buttonListener); alertDialog2.setOnClickListener(buttonListener); alertDialog3.setOnClickListener(buttonListener); } 

Next, we must finish the set event here, we want to look at the AlertDialog the basic usage as I said before AlertDialog into too many components so we only demonstrate the first of the three kinds is a very basic message + dialog button the second put the List into AlertDialog 
Third into a Layout AlertDialog it can enter text 

So we wrote an event, respectively, to determine the three dialog boxes 
 private OnClickListener buttonListener = new OnClickListener(){ @Override public void onClick(View view) { switch(view.getId()){ case R.id.alertdialog1: break; case R.id.alertdialog2: break; case R.id.alertdialog3: break; } } }; 

If you press a button, we add the following code 
 new AlertDialog.Builder(AlertDialogDemoActivity.this) .setTitle("午餐時間") .setMessage("要吃飯了嗎?") . setPositiveButton ("好", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "走吧!一起吃", Toast.LENGTH_SHORT).show(); } }) . setNegativeButton ("等下再吃", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "可是我好餓耶", Toast.LENGTH_SHORT).show(); } }) . setNeutralButton ("不餓", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你減肥嗎?", Toast.LENGTH_SHORT).show(); } }) .show(); 
This can generate three keys, each key will be handling press event if the event did not write any deal with things, is what did not make to close the window. 


Followed by a second processing event, we can throw into the string AlertDialog 
It will automatically become a List 
 final String[] lunch = { "雞腿飯","滷肉飯","排骨飯","蛋炒飯","水餃","陽春麵" }; new AlertDialog.Builder(AlertDialogDemoActivity.this) .setItems(lunch, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你今天吃的是"+lunch[which], Toast.LENGTH_SHORT).show(); } }) .show(); 

As long as you are in the onClick handler event, use the argument of which 
Can press array that Item 

Last one is a written Layout lost come so we must be ready for an xml 
Suppose we are only into the components of an EditText 
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/edittext" /> </LinearLayout> 

Then we can use the java files it 
 LayoutInflater inflater = LayoutInflater.from(AlertDialogDemoActivity.this); final View v = inflater.inflate(R.layout.alertdialog_edittext, null); 
And then AlertDialog throw into this View 
 new AlertDialog.Builder(AlertDialogDemoActivity.this) .setTitle("請輸入你的名字") . setView(v) .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText editText = (EditText) (v.findViewById(R.id.edittext)); Toast.makeText(getApplicationContext(), "你叫做"+ editText.getText().toString() , Toast.LENGTH_SHORT).show(); } }) .show(); 

As a result, when the jump out of the dialog box, enter your name program that can detect to the name you entered and displayed 

Code download 
http://uploadingit.com/file/waecbh74ctlimsf8/AlertDialogDemo.zip 

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © .Hacking Cracking Tricks And Tutorials, Paid Scripts, Latest Exploits, 0Day Vulnerability, - Skyblue - Powered by Blogger - Designed by Johanes Djogan -