Android AlertDialog自定义对话框的实现

废话少说,直接上图:

Android AlertDialog自定义对话框的实现

上图的具体实现:

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="10dp"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/image_id"
        android:layout_alignTop="@+id/image_id"
        android:layout_marginLeft="15dp"
        android:textSize="18dp"
        />
    <TextView
        android:id="@+id/mood"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:layout_toRightOf="@+id/image_id"
        android:layout_marginLeft="15dp"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/image_id"
        android:text="电量:"
        android:layout_marginLeft="227dp"/>
    <TextView
        android:id="@+id/batty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="260dp"
        android:layout_marginTop="15dp"
        />

</RelativeLayout>

activity_main.xml中的布局就是一个button,此处略。

MainActivity.java

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
   String[] nameString = {"5357277","2017196","2017587"};
   int[] image = {R.drawable.wifigreen,R.drawable.wifigreen,R.drawable.wifigreen};
   String[] moodString = {"空闲","运行","空闲"};
   int[] batty = {86,50,10};
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initEvent();
   }

   /**
    * 初始化点击事件
    */
   private void initEvent() {

      findViewById(R.id.dialog_btn4).setOnClickListener(
            new OnClickListener() {
               @Override
               public void onClick(View v) {
                  // TODO Auto-generated method stub
                  showDialog4();
               }
            });
   }

    /**
    * 显示列表对话框
    */
   private void showDialog4() {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("设备列表");//设置标题
      builder.setIcon(R.drawable.route);//设置图标
      final MyAdapter myAdapter = new MyAdapter(MainActivity.this,image,nameString,moodString,batty);
      builder.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "我动了"+myAdapter.getItem(i)+"!",
                  Toast.LENGTH_SHORT).show();
         }
      });
      AlertDialog dialog = builder.create();//创建dialog
      dialog.show();//显示对话框
   }

MyAdapter.java

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private String nameString[];
    private int image[];
    private String moodString[];
    private int batty[];

    public MyAdapter(Context context,int[] image_Objects,String[] name_Strings,String[] mood_String,int[] batty){
        this.layoutInflater = LayoutInflater.from(context);
        this.nameString     = name_Strings;
        this.moodString     = mood_String;
        this.image          = image_Objects;
        this.batty          = batty;
    }

    @Override
    public int getCount() {
        return nameString.length;
    }

    @Override
    public Object getItem(int position) {
        return nameString[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = layoutInflater.inflate(R.layout.item,null);
        MyClass myClass = new MyClass();
        myClass.imageView = (ImageView) convertView.findViewById(R.id.image_id);
        myClass.nameView  = (TextView) convertView.findViewById(R.id.name);
        myClass.moodView  = (TextView) convertView.findViewById(R.id.mood);
        myClass.battyView = (TextView) convertView.findViewById(R.id.batty);

        myClass.imageView.setBackgroundResource(image[position]);
        myClass.nameView.setText(nameString[position]);
        myClass.moodView.setText(moodString[position]);
        myClass.battyView.setText(Integer.toString(batty[position]));
        myClass.nameView.setTextColor(Color.BLACK);
        myClass.moodView.setTextColor(Color.RED);
        if (batty[position] <70 && batty[position]>40){
            myClass.battyView.setTextColor(Color.GRAY);
        }else if (batty[position]<=40&& batty[position]>0){
            myClass.battyView.setTextColor(Color.RED);
        }else {
            myClass.battyView.setTextColor(Color.GREEN);
        }


        return convertView;
    }

    class MyClass{
        ImageView imageView;
        TextView nameView;
        TextView moodView;
        TextView battyView;
    }
}
item.xml中的布局根据自己的情况来写,以实现自定义列表对话框,搞定!