您的位置:首页 > 移动开发 > Android开发

Android中的dialog窗口

2016-04-11 16:35 447 查看
String [] ss=new String[]{

    "北京" ,

       "上海",

    "杭州"

     };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.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();

        if (id == R.id.action_settings) {

            return true;

        }

        return super.onOptionsItemSelected(item);

    }

    //提示

    public void test(View view){

    AlertDialog.Builder builder=new Builder(this);

    builder.setTitle("消息提示");

    builder.setMessage("约不约?");

    builder.setPositiveButton("确定", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "确定",0).show();
}
});

    builder.setNegativeButton("取消",new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "取消",0).show();
}
});

    builder.setNeutralButton("查看详情",new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "查看详情",0).show();
}
});

    AlertDialog alertDialog=builder.create();

    alertDialog.show();

    }

    

    

    //单选

    public void test1(View view) {
AlertDialog.Builder builder=new Builder(this);
builder.setTitle("你所在的城市");
builder.setSingleChoiceItems(ss, 0,new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
     Toast.makeText(MainActivity.this,ss[which],0).show();
}
});
AlertDialog alertDialog=builder.create();//先加载完,后展示出来;
alertDialog.show();
}

    

   / /多选

    public void test2(View view) {

    AlertDialog.Builder builder=new Builder(this);

    builder.setTitle("可以多选");

    builder.setMultiChoiceItems(ss,new boolean[]{true,false,true} , new OnMultiChoiceClickListener() {//此处new boolen 可以改为null,默认没有选中

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,ss[which],0).show();
}
});

    AlertDialog alertDialog=builder.create();

    alertDialog.show();
}

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android