实现简单的选择头像的小程序

来源:互联网 发布:复旦大数据学院怎么样 编辑:程序博客网 时间:2024/05/17 23:04
package com.example.dialog1;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager.LayoutParams;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageButton;import android.widget.ImageView;public class MainActivity extends Activity {private Gallery gallery;private ImageButton imageButton;private int[] mImageIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,R.drawable.sample_thumb_5, };int index = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageButton = (ImageButton) findViewById(R.id.imageButton1);imageButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubAlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);alertDialog.setTitle("请选择头像:");LayoutInflater layoutInflater = getLayoutInflater();View view1 = layoutInflater.inflate(R.layout.xuanze, null);alertDialog.setView(view1);gallery = (Gallery) view1.findViewById(R.id.gallery1);gallery.setAdapter(new ImageAdapter());gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stubindex = arg2;}});alertDialog.setPositiveButton("确定", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub//removeDialog(index);imageButton.setImageResource(mImageIds[index]);}});alertDialog.setNegativeButton("取消", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubremoveDialog(index);}});alertDialog.create().show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}private class ImageAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn mImageIds.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView imageView = new ImageView(MainActivity.this);imageView.setImageResource(mImageIds[position]);imageView.setAdjustViewBounds(true);// 设置固定大小imageView.setMaxHeight(80);imageView.setMaxWidth(120);imageView.setLayoutParams(new Gallery.LayoutParams(50,50));imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);// imageView.setBackgroundResource(mGalleryItemBackground);return imageView;}}}


 

 

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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/gallery1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:spacing="10dp"         android:background="?android:galleryItemBackground"         android:gravity="center_vertical"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        />    <ImageSwitcher        android:id="@+id/imageSwitcher1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/gallery1"        android:layout_alignParentTop="true"        android:layout_marginTop="84dp" >    </ImageSwitcher></RelativeLayout>


 

 

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageButton        android:id="@+id/imageButton1"        android:onClick="popDialog"        android:layout_width="60dp"        android:layout_height="60dp"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:src="@drawable/sample_thumb_0" />    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/imageButton1"        android:layout_marginLeft="41dp"        android:layout_toRightOf="@+id/imageButton1"        android:ems="10"        android:inputType="text" >        <requestFocus />    </EditText></RelativeLayout>


 

原创粉丝点击