首先是MainActivity里面的代码
package com.example.czj01.alertdialoglist;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* 演示alertdialog不同的列表形式
*/
public class MainActivity extends Activity {
private Button btnColor,btnSetting;
private TextView textView;
private ButtonListener buttonListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
public void initView(){
btnColor = (Button) findViewById(R.id.btn_color);
textView = (TextView) findViewById(R.id.tvInfo);
btnSetting= (Button) findViewById(R.id.btn_setting);
}
public void setListener(){
buttonListener = new ButtonListener();
btnColor.setOnClickListener(buttonListener);
btnSetting.setOnClickListener(buttonListener);
}
/**
* 展示适配器列表dialog
*/
public void showAdapterListDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("设置字体颜色");
builder.setIcon(R.mipmap.ic_launcher);
//获取名称数组
String[] stringNames=getResources().getStringArray(R.array.setting_names);
//获取图片数组
String[] stringImages=getResources().getStringArray(R.array.setting_images);
List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
for(int i=0;i<stringNames.length;i++){
Map<String,Object> map=new HashMap<String,Object>();
map.put("setting",stringNames[i]);
//getIdentifier三个参数,一个是数组的id,一个“drawable”,一个getPackageName();
int imageId=getResources().getIdentifier(stringImages[i],"drawable",getPackageName());
map.put("image",imageId);
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.list_item,
new String[]{"setting","image"},
new int[]{R.id.tv_name,R.id.iv_logo});
//加载adapter对象,添加监听事件
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = null;
switch (which){
case 0://wifi
intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
break;
case 1://蓝牙设置
intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
break;
case 2://声音设置
intent = new Intent(Settings.ACTION_SOUND_SETTINGS);
break;
case 3://日期设置
intent = new Intent(Settings.ACTION_DATE_SETTINGS);
break;
}
startActivity(intent);
}
});
builder.create().show();
}
class ButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_color:
showCommonListDialog();
break;
case R.id.btn_setting:
showAdapterListDialog();
break;
}
}
}
/**
* 展示普通列表的dialog
*/
public void showCommonListDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("设置字体颜色");
builder.setIcon(R.mipmap.ic_launcher);
//数据源的资源id,点击事件的监听
builder.setItems(R.array.color_name, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
textView.setTextColor(Color.RED);
break;
case 1:
textView.setTextColor(Color.BLUE);
break;
case 2:
textView.setTextColor(Color.GREEN);
break;
}
}
});
builder.create().show();
}
}
然后是mainactivity的layout的布局文件xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="com.example.czj01.alertdialoglist.MainActivity">
<Button
android:id="@+id/btn_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_color"
android:background="#84b84e"
android:textSize="20sp"
android:textColor="#fff"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn_setting"
android:text="@string/str_setting"
style="@style/btn_style"
android:layout_below="@+id/btn_color"/>
<TextView
android:id="@+id/tvInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:background="#999"
android:gravity="center"
android:layout_below="@+id/btn_setting"
android:text="Hello World!"
/>
</RelativeLayout>
然后是设计的list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/btn_send"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"/>
</LinearLayout>
Button的style在xml文件里面有设置
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="btn_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#84b84e</item>
<item name="android:textSize">20sp</item>
<item name="android:textColor">#fff</item>
<item name="android:layout_marginTop">10dp</item>
</style>
</resources>
然后就是字符串string的xml设置
<resources>
<string name="app_name">AlertDialogList</string>
<string name="str_color">设置字体颜色</string>
<string name="str_setting">进行设置</string>
<string-array name="color_name">
<item>红色</item>
<item>绿色</item>
<item>蓝色</item>
</string-array>
<!--设置文本-->
<string-array name="setting_names">
<item>wifi设置</item>
<item>蓝牙设置</item>
<item>声音设置</item>
<item>日期设置</item>
</string-array>
<!--设置图片-->
<string-array name="setting_images">
<item>btn_send</item>
<item>btn_send</item>
<item>btn_send</item>
<item>btn_send</item>
</string-array>
</resources>
图片的信息,可以根据自己的需要,放在drawable文件里面,最终就形成了我们简单的alertdialog