ImageButton组件的应用
来源:互联网 发布:2017全国进出口数据 编辑:程序博客网 时间:2024/05/28 23:10
一:进行主界面的布局
<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:orientation="horizontal" > <ImageButton android:id="@+id/head" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/image0" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" /></LinearLayout>
二:新建dialog.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" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="60dp" android:background="#55000000" android:spacing="10dp" /></LinearLayout>
三:编写Activity,查找组件并进行相应的事件处理(点击imageButton弹出更改头像对话框)。
package com.lks.changeimage;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.res.TypedArray;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;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 implements OnItemClickListener{private ImageButton avatar;private Gallery gallery;private int[] images = { R.drawable.image1, R.drawable.image2,R.drawable.image3, R.drawable.image4,R.drawable.image5 };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);avatar = (ImageButton) this.findViewById(R.id.head);avatar.setOnClickListener(new OnClickListener() {public void onClick(View v) {LayoutInflater inflater = LayoutInflater.from(MainActivity.this);final View dialogView = inflater.inflate(R.layout.dialog, null);AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);gallery = (Gallery) dialogView.findViewById(R.id.gallery);gallery.setAdapter(new ImageAdapter());gallery.setSelection(images.length / 2);gallery.setOnItemClickListener(MainActivity.this);builder.setIcon(android.R.drawable.ic_dialog_dialer).setTitle("更改头像").setView(dialogView).setPositiveButton("确定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {}}).setNegativeButton("取消",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {}}).create().show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;} /** * 负责产生gallery中的图片 */private class ImageAdapter extends BaseAdapter {private int mGalleryItemBackground;public ImageAdapter(){TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0); typedArray.recycle();}// 返回图片的个数,比如你想得到图片的个数@Overridepublic int getCount() {return images.length;}@Overridepublic Object getItem(int position) {return images[position];}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView(MainActivity.this);imageView.setImageResource(images[position]);imageView.setBackgroundResource(mGalleryItemBackground);return imageView;}}@Overridepublic void onItemClick(AdapterView<?> adapterview, View view, int postion,long id) {avatar.setImageResource(images[postion]);}}
- ImageButton组件的应用
- ImageButton组件的应用(更改头像)
- ImageButton应用
- 杂乱之android的ImageButton应用
- Button,ImageButton,EditText三控件的应用。
- Android开发之ImageButton组件的使…
- android应用开发揭秘examples_04-16笔记(ImageButton的使用)
- ImageButton的坑 ImageButton 有问题
- Android 评分组件RatingBar、ImageView图片、 ImageButton图片按钮的综合运用
- aspjpeg组件的应用
- 组件的基本应用
- 选择组件的应用
- 日历组件的应用
- cxLookupComboBox 组件的应用
- GridView组件的应用
- RatingBar组件的应用
- ListView组件的应用
- cxLookupComboBox 组件的应用
- 短语、直接短语、句柄、素短语
- C#将datatable生成easyui的绑定tree 的json数据格式
- struts框架中使用tiles组件代码实例
- hdu 1429搜索
- c# 使用FileStream打开并清空文件、将一文件内容另存为到新文件
- ImageButton组件的应用
- 大数据量采用什么方式建表
- typedef define
- URL编码
- 【JAVA 基础】JAVA常见异常集锦(持续更新)
- ID3算法的python实现
- .net中[Serializable]序列化的应用
- cassandra (2)Understanding the Architecture【About internode communications (gossip)】
- c++题目