- Back to Home »
- Android »
- How to use ListDialog
Tuesday, June 26, 2012
Suppose you have a Button by going to want to jump out of a ListDialog how to write?
First create the button above in the xml
Then came the java files, this xml sets into
First of all, we first establish a wait for the next call Dialog,
First of all, we first of all to be rendered the item written on the inside of the onCreateDialog
Suppose written three projects, then a AlertDialog,
Then write the event these three items to throw into AlertDialog
And then press that item is displayed to the Toast
As a result, the listDialog you're done,
Write Button down event, it will jump out of us to do this the List the Dialog
As long as the onCreate inside with
We did not use the parameters of showDialog inside, so casually passed in any number can be
So all our features are here,
Complete code
You can see the following Figure
Code download
http://uploadingit.com/file/ehj1ijtor4ledk2r/DemoListDialog.zip
First create the button above in the 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:text="ListDialogDemo" android:id="@+id/listdialog_button" /> </LinearLayout>
Then came the java files, this xml sets into
package com.givemepass.demolistdialog; import android.app.Activity; import android.os.Bundle; public class DemoListDialogActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
First of all, we first establish a wait for the next call Dialog,
@Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub return super.onCreateDialog(id); }
First of all, we first of all to be rendered the item written on the inside of the onCreateDialog
CharSequence[] items = {"item1","item2","item3"};
Suppose written three projects, then a AlertDialog,
Then write the event these three items to throw into AlertDialog
And then press that item is displayed to the Toast
Builder builder = new AlertDialog.Builder(this); builder.setItems(items, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialogInterface,int which) {
switch(which){ case 0: Toast.makeText(DemoListDialogActivity.this, "item1", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(DemoListDialogActivity.this, "item2", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(DemoListDialogActivity.this, "item3", Toast.LENGTH_SHORT).show();
break;}}});
As a result, the listDialog you're done,
Write Button down event, it will jump out of us to do this the List the Dialog
As long as the onCreate inside with
private Button listDialogButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listDialogButton = (Button)findViewById(R.id.listdialog_button); listDialogButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { showDialog(0); } }); }
We did not use the parameters of showDialog inside, so casually passed in any number can be
So all our features are here,
Complete code
package com.givemepass.demolistdialog; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class DemoListDialogActivity extends Activity { private Button listDialogButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listDialogButton = (Button)findViewById(R.id.listdialog_button); listDialogButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { CharSequence[] items = {"item1","item2","item3"}; Builder builder = new AlertDialog.Builder(this); builder.setItems(items, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialogInterface,int which) { switch(which){ case 0: Toast.makeText(DemoListDialogActivity.this, "item1", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(DemoListDialogActivity.this, "item2", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(DemoListDialogActivity.this, "item3", Toast.LENGTH_SHORT).show(); break; } } }); return builder.create(); } }
You can see the following Figure
Code download
http://uploadingit.com/file/ehj1ijtor4ledk2r/DemoListDialog.zip