Android Studio 对话框
<小白一枚,大神绕道>
废话不多说,直接上代码
xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</android.support.constraint.ConstraintLayout>
java
package com.example.day1;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
lv = findViewById(R.id.lv);
List<HashMap<String, String>> list = new ArrayList<>();
for (int i = 1; i <= 9; i++) {
HashMap<String, String> map = new HashMap<>();
map.put("t",i+"");
list.add(map);
}
MyAdapter myAdapter = new MyAdapter(Main2Activity.this, list);
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
dialog1();
break;
case 1:
single_dialog2();
break;
case 2:
multit_dialog3();
break;
case 3:
dialog4();
break;
case 4:
progress_dialog5();
break;
case 5:
progress_dialog6();
break;
case 6:
date_dialog7();
break;
case 7:
time_dialog8();
break;
case 8:
//这里和本文章无关
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(intent);
finish();
break;
}
}
});
}
private void time_dialog8() {
Calendar calendar = Calendar.getInstance();
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
},calendar.get(calendar.HOUR),calendar.get(calendar.MINUTE),true).show();
}
private void date_dialog7() {
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
},calendar.get(Calendar.YEAR),calendar.get(calendar.MONTH),calendar.get(calendar.get(Calendar.DAY_OF_MONTH))).show();
}
private void progress_dialog6() {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置水平中央
dialog.setMax(100);
dialog.setMessage("正在下载,请稍后");
dialog.show();
CountDownTimer countDownTimer = new CountDownTimer(10000+1000, 1000) {
int count=0;
@Override
public void onTick(long millisUntilFinished) {
dialog.setProgress(count+=10);
}
@Override
public void onFinish() {
dialog.setMessage("已完成");
dialog.dismiss();
}
}.start();
}
private void progress_dialog5() {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置水平中央
dialog.setMax(100);
dialog.setMessage("正在下载,请稍后");
dialog.show();
CountDownTimer countDownTimer = new CountDownTimer(10000+1000, 1000) {
int count=0;
@Override
public void onTick(long millisUntilFinished) {
dialog.setProgress(count+=10);
}
@Override
public void onFinish() {
dialog.setMessage("已完成");
dialog.dismiss();
}
}.start();
}
private void dialog4() {
View view = LayoutInflater.from(this).inflate(R.layout.dialog2, null);
//构建者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//画画
builder.setTitle("标题");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了no",Toast.LENGTH_SHORT).show();
}
});
//设置布局
builder.setView(view);
//创建对话框
AlertDialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
private void multit_dialog3() {
final String[] str = {"a","b","c"};
final boolean[] flag = {true,false,true};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0;i<flag.length;i++){
if (flag[i]){
Toast.makeText(Main2Activity.this,"你点击了"+str[i],Toast.LENGTH_SHORT).show();
}
}
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了no",Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(str, flag, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flag[which] = isChecked;
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void single_dialog2() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了no",Toast.LENGTH_SHORT).show();
}
});
final String[] str = {"a","b","c"};
builder.setSingleChoiceItems(str, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了"+str[which],Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void dialog1() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("标题");
builder.setMessage("内容");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main2Activity.this,"你点击了no",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
自定义适配器(布局文件一个TextView就可以)
package com.example.day1;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MyAdapter extends BaseAdapter {
Context context;
List<HashMap<String,String>> list;
public MyAdapter(Context context, List<HashMap<String, String>> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
viewHolder = new ViewHolder();
convertView = View.inflate(context, R.layout.item, null);
viewHolder.t = convertView.findViewById(R.id.t);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = list.get(position);
if (convertView != null){
viewHolder.t.setText(map.get("t"));
}
return convertView;
}
public class ViewHolder{
TextView t;
}
}