先来看一下运行图:
下面看一下具体的代码:
MainActivity.java
package cn.bzu.dialog_imageswitcher; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageButton; import android.widget.ImageView; public class MainActivity extends Activity{ private ImageButton imageButton; private Gallery gallery; private ImageView imageView; private Integer[] mThumbIds = { R.drawable.aa, R.drawable.bb, R.drawable.cc, R.drawable.dd, R.drawable.ee, R.drawable.ff, R.drawable.gg}; private Integer[] mImageIds = { R.drawable.cc, R.drawable.dd, R.drawable.ee, R.drawable.ff, R.drawable.gg }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iniComponent(); imageButton.setImageResource(R.drawable.cc); imageButton.setOnClickListener(new ImageButtonListener()); } // 初始化组件 private void iniComponent() { imageButton = (ImageButton) findViewById(R.id.imageButton); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } // 创建适配器 private class ImageAdapter extends BaseAdapter { private Context context; public ImageAdapter(Context c) { this.context = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } // 获取视图 public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); imageView.setImageResource(mThumbIds[position]); imageView.setAdjustViewBounds(true); imageView.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; } } private class ImageButtonListener implements android.view.View.OnClickListener { public void onClick(View v) { AlertDialog.Builder imageSelect = new AlertDialog.Builder( MainActivity.this); LayoutInflater layoutInflater = LayoutInflater .from(MainActivity.this); View galleryView = layoutInflater.inflate( R.layout.activity_gallery, null); gallery = (Gallery) galleryView.findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(MainActivity.this)); gallery.setOnItemClickListener(new ItemClickListener()); imageSelect.setIcon(android.R.drawable.btn_star) .setView(galleryView).setTitle("头像选择") .setPositiveButton("确定", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).create().show(); } } private class ItemClickListener implements OnItemClickListener{ public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { AlertDialog.Builder imageSelect = new AlertDialog.Builder( MainActivity.this); LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this); View layoutview = layoutInflater.inflate(R.layout.activity_image, null); imageView = (ImageView) layoutview.findViewById(R.id.imageView); imageView.setImageResource(mImageIds[position]); imageSelect.setView(layoutview) .setPositiveButton("确定", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { imageButton.setImageResource(mThumbIds[position]); } }).create().show(); } } }
下面是布局文件:
activity_main.xml:
<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:background="@drawable/gh" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text="@string/prompt" tools:context=".MainActivity" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="@string/info" > <requestFocus /> </EditText> </LinearLayout> </LinearLayout>
activity_gallery.xml:<?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" android:orientation="vertical" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="2dp" /> </LinearLayout>
activity_image.xml:
<?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" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:contentDescription="@string/app_name" /> </LinearLayout>
必要的需要在strings.xml中配置一下就OK了!