Search This Blog

Wednesday, March 23, 2016

ALERTDIALOG - POPUP CHON YES NO

- File main.xml : 

<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"    
   tools:context=".AlertActivity" 

>

    <Button        
       android:id="@+id/one_button_alert"        
       android:layout_width="200dip"        
       android:layout_height="wrap_content"        
       android:layout_alignParentLeft="true"        
       android:layout_marginTop="25dip"        
       android:layout_marginLeft="20dip"        
       android:text="One Button Alert" />
    <Button        
      android:id="@+id/two_button_alert"        
      android:layout_width="200dip"        
      android:layout_height="wrap_content"        
      android:layout_alignParentLeft="true"        
      android:layout_below="@id/one_button_alert"        
      android:layout_marginLeft="20dip"        
      android:text="Two Button Alert" />
    <Button        
      android:id="@+id/three_button_alert"        
      android:layout_width="200dip"        
      android:layout_height="wrap_content"        
      android:layout_alignParentLeft="true"        
      android:layout_below="@id/two_button_alert"        
      android:layout_marginLeft="20dip"        
      android:text="Three Button Alert" />
</RelativeLayout>


- File main.java : 


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/** * @author Vienna * */public class ManHinhChinh extends Activity {
    private Button button1;
    private Button button2;
    private Button button3;

    @Override    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_man_hinh_chinh);
        button1 = (Button) findViewById(R.id.one_button_alert);
        button2 = (Button) findViewById(R.id.two_button_alert);
        button3 = (Button) findViewById(R.id.three_button_alert);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override            
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        ManHinhChinh.this);
                builder.setTitle("Sample Alert");
                builder.setMessage("Sample One Action Button Alert");
                builder.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Log.e("info", "OK");
                            }
                        });
                builder.show();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override            
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        ManHinhChinh.this);
                builder.setTitle("Sample Alert");
                builder.setMessage("Sample Two Action Button Alert");
                builder.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Log.e("info", "NO");
                            }
                        });
                builder.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Log.e("info", "YES");
                                Toast.makeText(getApplication(),"YES",Toast.LENGTH_LONG).show();
                            }
                        });
                builder.show();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override            
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        ManHinhChinh.this);
                builder.setTitle("Sample Alert");
                builder.setMessage("Sample Three Action Button Alert");
                builder.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Log.e("info", "NO");
                            }
                        });
                builder.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Log.e("info", "YES");
                            }
                        });
                builder.setNeutralButton("CANCEL",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Log.e("info", "CANCEL");
                            }
                        });

                builder.show();
            }
        });
    }

}





No comments:

Post a Comment