lunes, 16 de marzo de 2015

Ventana Dialogo en Android Studio

1.- Creamos un proyecto nuevo a continuacion el codigo fuente de el diseño y posteriormente el codigo de la aplicacion.

activity_main.xml



    

Main_Activity.java


package com.master.kenny.ventanadialogo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    //Creo una secuencia de elementos para mostrar en el dialogo
    CharSequence[] items = { "One Piece" , "Pokemon" , "Dragon Ball Z"};
    boolean[] itemChecked = new boolean[ items.length ];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //Creo el evento onClick
    public void onClick( View v )
    {
        showDialog( 0 );
    }

    @Override
    protected Dialog onCreateDialog( int id )
    {
        switch( id )
        {
            case 0:
                return new AlertDialog.Builder( this )
                        .setIcon( R.drawable.ic_launcher )
                        .setTitle("Este es un dialogo con un simple texto")
                        .setPositiveButton("OK" ,
                                new DialogInterface.OnClickListener()
                                {
                                    public void onClick( DialogInterface dialog , int whichButton )
                                    {
                                        Toast.makeText( getBaseContext() ,
                                                "Presionado OK" , Toast.LENGTH_SHORT ).show();
                                    }
                                }
                        )
                        .setNegativeButton("Cancelar" ,
                                new DialogInterface.OnClickListener()
                                {
                                    public void onClick( DialogInterface dialog , int whichButton )
                                    {
                                        Toast.makeText( getBaseContext() , "Presionado Cancelar" , Toast.LENGTH_SHORT ).show();
                                    }
                                }
                        ).setMultiChoiceItems( items , itemChecked ,
                                new DialogInterface.OnMultiChoiceClickListener()
                                {
                                    public void onClick( DialogInterface dialog ,
                                                         int which , boolean isChecked )
                                    {
                                        Toast.makeText( getBaseContext() , items[ which ] +
                                                ( isChecked ? " seleccionado" : " deseleccionado") ,
                                                Toast.LENGTH_SHORT ).show();
                                    }
                                }
                        ).create();
        }

        return null;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


No hay comentarios:

Publicar un comentario