Cách kiếm tiền đơn giản, hiệu quả nhất

Thứ Bảy, 4 tháng 7, 2015

Displaying a dialog using an activity in android tutorial

Displaying a dialog using an activity in android tutorial.

There are times where you need to display a dialog window to get a confirmation from the user. In 
this case, you can override the onCreateDialog()protected method defined in the base Activity
class to display a dialog window. The following Try It Out shows you how.
Now, create a new android project.
Edit interface in activity_main.xml file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.demoshowdialog.MainActivity" >

    <Button
        android:id="@+id/btnShow_Dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Dialog" />

</RelativeLayout>
Next, the proccess code in MainActivity.java file:

package com.example.demoshowdialog;

import android.app.Activity;
import android.app.AlertDialog;
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;

public class MainActivity extends Activity {
       private Button btnShow_Dialog;
       CharSequence[] items = { "Asus", "Acer", "Dell", "Vaio" };
       boolean[] bool = new boolean[items.length];

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          btnShow_Dialog = (Button) findViewById(R.id.btnShow_Dialog);
          btnShow_Dialog.setOnClickListener(new OnClickListener() {

             @Override
             public void onClick(View v) {
                 showDialog(0);
             }
          });
       }

       @Override
       protected Dialog onCreateDialog(int id) {
          // TODO Auto-generated method stub
          switch (id) {
          case 0:
              return new AlertDialog.Builder(this)
              .setIcon(R.drawable.ic_launcher)
              .setTitle("Show Dialog Demo")
              .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialog,int which) {
                             // TODO Auto-generated method stub
                             }
                   })
              .setNegativeButton("No",new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface dialogint which) {
                             // TODO Auto-generated method stub
                             }
                   })
              .setMultiChoiceItems(items, bool,new DialogInterface.OnMultiChoiceClickListener() {

              @Override
              public void onClick(DialogInterface dialog,int which, boolean isChecked) {
                                                              // TODO Auto-generated method stub
                    }
              }).create();
              }
              return null;
       }
}
Explain:
To display a dialog, you first override the onCreateDialog() method in the Activity class
@Override
       protected Dialog onCreateDialog(int id) {
...
}
This method is called when you call the showDialog() method
btnShow_Dialog = (Button) findViewById(R.id.btnShow_Dialog);
              btnShow_Dialog.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           showDialog(0);
                     }
              });

The onCreateDialog() method is a callback for creating dialogs that are managed by the activity. When you call the showDialog() method, this callback will be invoked. The showDialog() method accepts an integer argument identifying a particular dialog to display.
Download source code: updating...

1 nhận xét:

Đăng nhận xét