Android中的dialog窗口

来源:互联网 发布:hp3055网络打印机驱动 编辑:程序博客网 时间:2024/06/05 11:08
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();
}
  }
0 0
原创粉丝点击