首页 > 代码库 > 安卓学习-界面-ui-对话框

安卓学习-界面-ui-对话框

1.最简单的消息提示

 

 Button btn=(Button)findViewById(R.id.button1);    btn.setOnClickListener(new OnClickListener() {        public void onClick(View v) {            AlertDialog d=new AlertDialog.Builder(MainActivity.this)            .setTitle("这是标题")        //图标        .setIcon(R.drawable.ic_launcher)        .setMessage("消息提示")        .setNegativeButton("左按钮", null)        .setNeutralButton("中按钮", null)        .setPositiveButton("右按钮", null)                    .create();        d.show();    }});

 2.简单列表窗口

public class MainActivity extends Activity {    String[] items=new String[]{"苹果","鸭梨","桔子","香蕉"};        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 Button btn=(Button)findViewById(R.id.button1);            btn.setOnClickListener(new OnClickListener() {                public void onClick(View v) {                    AlertDialog d=new AlertDialog.Builder(MainActivity.this)                    .setTitle("水果列表")                    //图标                    .setIcon(R.drawable.ic_launcher)                    .setItems(items, new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialog, int which) {                            Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();                                                    }                    })                    .setNeutralButton("我知道了", new DialogInterface.OnClickListener() {                                public void onClick(DialogInterface dialog, int which) {                            Toast.makeText(MainActivity.this,"我知道了", Toast.LENGTH_SHORT).show();                            }                    })                    .create();                d.show();            }        });    }}
View Code

 

3.单选列表框

public class MainActivity extends Activity {    int selIndex=0;    String[] items=new String[]{"苹果","鸭梨","桔子","香蕉"};        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 Button btn=(Button)findViewById(R.id.button1);            btn.setOnClickListener(new OnClickListener() {                public void onClick(View v) {                    AlertDialog d=new AlertDialog.Builder(MainActivity.this)                    .setTitle("请选择")                    //图标                    .setIcon(R.drawable.ic_launcher)                    .setSingleChoiceItems(items, selIndex, new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int which) {                            selIndex=which;                            Toast.makeText(MainActivity.this, "你选择的是:"+items[which], Toast.LENGTH_SHORT).show();                                    }                    })                    .setNeutralButton("确定", new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int which) {                            Toast.makeText(MainActivity.this,"确定,选择的的是"+items[selIndex], Toast.LENGTH_SHORT).show();                            }                    })                    .setPositiveButton("取消", new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int which) {                            Toast.makeText(MainActivity.this,"取消", Toast.LENGTH_SHORT).show();                            }                    })                    .create();                d.show();            }        });    }}
View Code

 4.多选

 

public class MainActivity extends Activity {    int selIndex=0;    String[] items=new String[]{"苹果","鸭梨","桔子","香蕉"};    boolean[] selItems=new boolean[]{false,false,true,true};        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                 Button btn=(Button)findViewById(R.id.button1);            btn.setOnClickListener(new OnClickListener() {                public void onClick(View v) {                    AlertDialog d=new AlertDialog.Builder(MainActivity.this)                    .setTitle("请选择")                    //图标                    .setIcon(R.drawable.ic_launcher)                    .setMultiChoiceItems(items,selItems, new DialogInterface.OnMultiChoiceClickListener() {                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {                            if(isChecked){                                Toast.makeText(MainActivity.this, "你选择了:"+items[which], Toast.LENGTH_SHORT).show();                                selItems[which]=isChecked;                            }                        }                    })                    .setNeutralButton("确定", new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int which) {                            String str="";                            for(int i=0;i<selItems.length;i++){                                if(selItems[i]){                                    str=str+items[i]+",";                                }                            }                            Toast.makeText(MainActivity.this,"确定,选择的的是"+str, Toast.LENGTH_SHORT).show();                            }                    })                    .setPositiveButton("取消", new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int which) {                            Toast.makeText(MainActivity.this,"取消", Toast.LENGTH_SHORT).show();                            }                    })                    .create();                d.show();            }        });    }}
View Code

5.

安卓学习-界面-ui-对话框