<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是简单的Dialog"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是选择的Dialog"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是单选的Dialog"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是多选的Dialog"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是自定义的Dialog"/>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是Date的Dialog"/>
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是Time的Dialog"/>
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="popupwindow"/>
</LinearLayout>
####自定义dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_background">
<TextView
android:id="@+id/textview_tittle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是标题"
style="@style/MytextView" />
<TextView
android:id="@+id/textview_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是内容"
android:padding="10dp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/but_background"
android:text="取消"/>
<Button
android:id="@+id/button_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/but_background"
android:text="确定"/>
</LinearLayout>
</LinearLayout>
####自定义popupwindow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="文本1"/>
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="文本1"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton1;
private Button mButton2;
private Button mButton3;
private String sex;
private String [] mSex={"男","女","其他"};
private String[] mData={"我是第一条","我是第二条","我是第三条","我是第四条","我是第五条"};
private Button mButton4;
private String[] mHobby={"吃饭","睡觉","唱歌","游泳","打球"};
private boolean [] mIsCheckedHobby=new boolean[mHobby.length];
private StringBuffer buffer;
private Button mButton5;
private Button mButton6;
private Calendar mCalendar;
private Button mButton7;
private Button mButton8;
private PopupWindow popup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1=(Button)findViewById(R.id.button1);
mButton2=(Button)findViewById(R.id.button2);
mButton3=(Button)findViewById(R.id.button3);
mButton4=(Button)findViewById(R.id.button4);
mButton5=(Button)findViewById(R.id.button5);
mButton6=(Button)findViewById(R.id.button6);
mButton7=(Button)findViewById(R.id.button7);
mButton8=(Button)findViewById(R.id.button8);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mButton5.setOnClickListener(this);
mButton6.setOnClickListener(this);
mButton7.setOnClickListener(this);
mButton8.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
showDialog();
break;
case R.id.button2:
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setItems(mData, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplication(),"选中了第"+which+"条",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog=builder.create();
dialog.show();
break;
case R.id.button3:
simpleShow();
break;
case R.id.button4:
AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
builder1.setTitle("我的爱好");
builder1.setMultiChoiceItems(mHobby, mIsCheckedHobby, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mIsCheckedHobby[which]=isChecked;
Toast.makeText(getApplication(),"我选择爱好是"+mHobby[which],Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder1.setNeutralButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
buffer=new StringBuffer();
for (int i=0;i<mHobby.length;i++){
if(mIsCheckedHobby[i])
buffer.append(mHobby[i]);
}
mButton4.setText("我的爱好是"+buffer);
}
});
AlertDialog dialog1=builder1.create();
dialog1.show();
break;
case R.id.button5:
final Dialog dialog3 = new Dialog(MainActivity.this,R.style.NoDialogTitle);
LayoutInflater inflater=getLayoutInflater();
View dialogview=inflater.inflate(R.layout.activity_dialog, null);
Button button_cancel= (Button) dialogview.findViewById(R.id.button_cancel);
Button button_ok= (Button) dialogview.findViewById(R.id.button_ok);
button_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog3.dismiss();
}
});
button_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"请确认!",Toast.LENGTH_SHORT).show();
dialog3.dismiss();
}
});
// dialog3.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消dialog的默认标题
dialog3.setContentView(dialogview);
dialog3.show();
break;
case R.id.button6:
mCalendar=Calendar.getInstance();
DatePickerDialog pickerDialog =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set(year,monthOfYear,dayOfMonth);
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
Toast.makeText(getApplicationContext(), "现在时间是"+ format.format(mCalendar.getTime()), Toast.LENGTH_SHORT).show();
}
},mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));
pickerDialog.show();
break;
case R.id.button7:
mCalendar=Calendar.getInstance();
TimePickerDialog timeDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR,hourOfDay);
mCalendar.set(Calendar.MINUTE,minute);
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日HH:mm");
Toast.makeText(getApplicationContext(),"北京时间:"+format.format(mCalendar.getTime()),Toast.LENGTH_SHORT).show();
}
},mCalendar.get(Calendar.HOUR),mCalendar.get(Calendar.MINUTE),true);
timeDialog.show();
break;
case R.id.button8:
popup=new PopupWindow(MainActivity.this);
LayoutInflater inflater1=getLayoutInflater();
View view=inflater1.inflate(R.layout.activity_popupwindow, null);
popup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setContentView(view);
popup.showAsDropDown(mButton8);
popup.setFocusable(false);
popup.setOutsideTouchable(true);
break;
default:
break;
}
}
//设置返回按钮
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
if (popup!=null&&popup.isShowing())
popup.dismiss();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void simpleShow() {
AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
builder1.setSingleChoiceItems(mSex,0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sex=mSex[which];
Toast.makeText(getApplicationContext(), "你选择的性别是:" + mSex[which], Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButton3.setText("您的性别是"+sex);
}
});
builder1.show();
}
private void showDialog() {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("我是内容");
builder.setTitle("我是标题");
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog=builder.create();
dialog.show();
}