当前位置: 代码迷 >> Android >> Android 仿照发说说
  详细解决方案

Android 仿照发说说

热度:286   发布时间:2016-04-24 11:16:27.0
Android 模仿发说说

本片博客的事例是根据我自己项目中的部分需求来的,所以有些和这个不相关的内容和源码,大家可以忽略不计。这种发说说的功能,我也是折腾了很久,今日才得知道,大神请不要见笑,,l里面的图片自己找好了,好了,先看效果:



property_repair,xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:id="@+id/container"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@color/main_background"  tools:ignore="MergeRootFrame" >    <RelativeLayout      android:id="@+id/Layout_top"      android:orientation="horizontal"       android:layout_width="fill_parent"      android:layout_height="40dp"       android:layout_alignParentTop="true"      android:gravity="center">  <include android:id="@+id/shopcar_head" layout="@layout/elife_head" />  </RelativeLayout>    <RelativeLayout      android:id="@+id/Layout_bottom"       android:layout_alignParentBottom="true"    android:layout_width="fill_parent"           android:layout_height="40dp"    android:gravity="center" >      <Button        android:id="@+id/button1"        android:layout_width="100dp"        android:layout_height="fill_parent"      android:textSize="15sp"      android:text="提交问题"       android:textColor="#ffffff"      android:background="@drawable/round"/>      <Button        android:id="@+id/button2"        android:layout_width="100dp"        android:layout_height="fill_parent"      android:layout_toRightOf="@+id/button1"      android:textSize="15sp"      android:layout_marginLeft="2dp"       android:textColor="#ffffff"   android:background="@drawable/round"      android:text="取消报修" />  </RelativeLayout>     <RelativeLayout      android:id="@+id/Content_Layout"	     android:layout_width="fill_parent"       android:layout_height="fill_parent"      android:layout_above="@id/Layout_bottom"       android:layout_below="@id/Layout_top"	    android:gravity="center">	     <LinearLayout         android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"      android:layout_alignParentBottom="true" >       <EditText         android:id="@+id/editText1"        android:layout_height="120dp"        android:layout_width="fill_parent"        android:textColor="#1eb2ff"        android:layout_margin="12dp"        android:gravity="top"        android:textSize="15sp"        android:hint="请说明你的问题..."        android:maxLength="500"        android:singleLine="false"        android:background="@drawable/editview_shape" />       <GridView        android:id="@+id/gridView1"        android:layout_width="fill_parent"        android:layout_height="200dp"        android:layout_margin="10dp"        android:background="#EFDFDF"        android:horizontalSpacing="5dp"        android:verticalSpacing="5dp"        android:numColumns="4"        android:scrollbars="none"        android:columnWidth="90dp"        android:stretchMode="columnWidth"        android:gravity="center" >      </GridView>      <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="(长按图片可以删除已添加图片)"        android:textColor="#1eb2ff"        android:gravity="center" />    </LinearLayout>  </RelativeLayout>  </RelativeLayout>

griditem_addpic.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:gravity="center"  android:descendantFocusability="blocksDescendants"  android:orientation="vertical" >  <RelativeLayout    android:layout_gravity="center"    android:layout_width="80dp"    android:layout_height="80dp"    android:orientation="vertical" >    <ImageView    		android:layout_marginTop="10dp"    		android:layout_marginRight="10dp"      android:id="@+id/imag_oo"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:scaleType="fitXY"      android:src="@drawable/ic_launcher" />  </RelativeLayout></LinearLayout>

editview_shape.xml  //这个是设置圆角的,因为我有这个需求,大家不要可以选择略过

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle"     android:padding="10dp">  <!-- 填充editView的颜色 -->  	<soild android:color="#ffffff"/>  	<!-- 设置圆角的弧度,radius半径越大,editView的边角越圆 -->   	<corners    	    android:radius="15dp"   	    android:bottomRightRadius="15dp"   	    android:bottomLeftRadius="15dp"   	    android:topLeftRadius="15dp"          android:topRightRadius="15dp"/>   	<stroke   	    android:color="#1eb2ff"          android:width="4px" /> </shape>

tijiao_pingjia.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:background="@null"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/tuuikuan_linea"        android:layout_width="200dp"        android:layout_height="100dp"        android:background="@drawable/round_1"        android:orientation="vertical" >        <TextView            android:id="@+id/shifou"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:gravity="center"            android:text="是否取消报修"            android:textColor="#1eb2ff"            android:textSize="12sp" />        <LinearLayout            android:id="@+id/xuanze"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:orientation="horizontal" >            <Button                android:id="@+id/yes"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_weight="1"                android:background="@null"                android:focusable="true"                android:gravity="center"                android:text="是"                android:textColor="#1eb2ff"                android:textSize="12sp" />            <Button                android:id="@+id/no"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_weight="1"                android:background="@null"                android:focusable="true"                android:gravity="center"                android:text="取消"                android:textColor="#1eb2ff"                android:textSize="12sp" />        </LinearLayout>    </LinearLayout></LinearLayout>


下面是程序 我因为项目需求,所有我的不是MainActivity,而是我需求的一个类,大家根据情况自己定义

Property_repair.java

package com.elife.property;import java.util.ArrayList;import java.util.HashMap;import com.elife.app.R;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.content.Intent;import android.content.pm.ActivityInfo;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.WindowManager;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.GridView;import android.widget.ImageView;import android.widget.RadioButton;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.SimpleAdapter.ViewBinder;import android.widget.Toast;public class Property_repair extends Activity {  private GridView gridView1;              //网格显示缩略图  private Button buttonPublish,button2,no,yes,button1;            //发布按钮  private final int IMAGE_OPEN = 1;        //打开图片标记  private String pathImage;                //选择图片路径  private Bitmap bmp;                      //导入临时图片  private ArrayList<HashMap<String, Object>> imageItem;  private SimpleAdapter simpleAdapter;     //适配器  private View view;  	private RadioButton radio;	private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.property_repair);        /*         * 防止键盘挡住输入框         * 不希望遮挡设置activity属性 android:windowSoftInputMode="adjustPan"         * 希望动态调整高度 android:windowSoftInputMode="adjustResize"         */        getWindow().setSoftInputMode(WindowManager.LayoutParams.        		SOFT_INPUT_ADJUST_PAN);        //锁定屏幕        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        setContentView(R.layout.property_repair);        radio = (RadioButton) findViewById(R.id.head_back);		tv = (TextView) findViewById(R.id.radio_text);		tv.setText("物业报修");		radio.setOnClickListener(new View.OnClickListener() {			@Override			public void onClick(View v) {				finish();			}		});        button2=(Button) findViewById(R.id.button2);        button2.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				view=LayoutInflater.from(Property_repair.this).inflate(R.layout.tijiao_pingjia, null);				final Dialog dialog=new Dialog(Property_repair.this,R.style.dialog2);				dialog.setContentView(view);				dialog.show();						no=(Button) view.findViewById(R.id.no);				yes=(Button) view.findViewById(R.id.yes);				yes.setOnClickListener(new OnClickListener() {										@Override					public void onClick(View v) {											finish();					}				});				no.setOnClickListener(new OnClickListener() {										@Override					public void onClick(View v) {						dialog.cancel();					}				});			}		});                 final EditText editText1=(EditText) findViewById(R.id.editText1);        button1=(Button) findViewById(R.id.button1);        button1.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {								if(editText1.getText().toString().length()==0){										Toast.makeText(Property_repair.this, "请简述详情", Toast.LENGTH_SHORT).show();														}else{					Toast.makeText(Property_repair.this, "提交成功", Toast.LENGTH_SHORT).show();						finish();				}			}		});        	        //获取控件对象        gridView1 = (GridView) findViewById(R.id.gridView1);        /*         * 载入默认图片添加图片加号         * 通过适配器实现         * SimpleAdapter参数imageItem为数据源 R.layout.griditem_addpic为布局         */        //获取资源图片加号        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon_addpic_unfocused);        imageItem = new ArrayList<HashMap<String, Object>>();        HashMap<String, Object> map = new HashMap<String, Object>();        map.put("imag_oo", bmp);        imageItem.add(map);        simpleAdapter = new SimpleAdapter(this,         		imageItem, R.layout.griditem_addpic,                 new String[] { "imag_oo"}, new int[] { R.id.imag_oo});         /*         * HashMap载入bmp图片在GridView中不显示,但是如果载入资源ID能显示 如         * map.put("itemImage", R.drawable.img);         * 解决方法:         *              1.自定义继承BaseAdapter实现         *              2.ViewBinder()接口实现         *  参考 http://blog.csdn.net/admin_/article/details/7257901         */        simpleAdapter.setViewBinder(new ViewBinder() {          @Override          public boolean setViewValue(View view, Object data,                  String textRepresentation) {              // TODO Auto-generated method stub              if(view instanceof ImageView && data instanceof Bitmap){                  ImageView i = (ImageView)view;                  i.setImageBitmap((Bitmap) data);                  return true;              }              return false;          }        });          gridView1.setAdapter(simpleAdapter);                /*         * 监听GridView点击事件         * 报错:该函数必须抽象方法 故需要手动导入import android.view.View;         */        gridView1.setOnItemClickListener(new OnItemClickListener() {  			@Override      public void onItemClick(AdapterView<?> parent, View v, int position, long id)      {  				if( imageItem.size() == 10) { //第一张为默认图片  					Toast.makeText(Property_repair.this, "图片数9张已满", Toast.LENGTH_SHORT).show();  				}  				else if(position == 0) { //点击图片位置为+ 0对应0张图片  				  					//选择图片  					Intent intent = new Intent(Intent.ACTION_PICK,         	                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);    	                startActivityForResult(intent, IMAGE_OPEN);    	              				}  				else {  					dialog(position);  				}      }  		});      }        //获取图片路径 响应startActivityForResult      protected void onActivityResult(int requestCode, int resultCode, Intent data) {          super.onActivityResult(requestCode, resultCode, data);                //打开图片          if(resultCode==RESULT_OK && requestCode==IMAGE_OPEN) {                    Uri uri = data.getData();              if (!TextUtils.isEmpty(uri.getAuthority())) {                  //查询选择图片                  Cursor cursor = getContentResolver().query(                          uri,                          new String[] { MediaStore.Images.Media.DATA },                          null,                           null,                           null);                  //返回 没找到选择图片                  if (null == cursor) {                      return;                  }                  //光标移动至开头 获取图片路径                  cursor.moveToFirst();                  pathImage = cursor.getString(cursor                          .getColumnIndex(MediaStore.Images.Media.DATA));              }        }  //end if 打开图片    }        //刷新图片    @Override  protected void onResume() {    super.onResume();    if(!TextUtils.isEmpty(pathImage)){      Bitmap addbmp=BitmapFactory.decodeFile(pathImage);      HashMap<String, Object> map = new HashMap<String, Object>();          map.put("imag_oo", addbmp);          imageItem.add(map);          simpleAdapter = new SimpleAdapter(this,           		imageItem, R.layout.griditem_addpic,                   new String[] { "imag_oo"}, new int[] { R.id.imag_oo});           simpleAdapter.setViewBinder(new ViewBinder() {            @Override            public boolean setViewValue(View view, Object data,                    String textRepresentation) {                // TODO Auto-generated method stub                if(view instanceof ImageView && data instanceof Bitmap){                    ImageView i = (ImageView)view;                    i.setImageBitmap((Bitmap) data);                    return true;                }                return false;            }          });           gridView1.setAdapter(simpleAdapter);          simpleAdapter.notifyDataSetChanged();      //刷新后释放防止手机休眠后自动添加          pathImage = null;    }  }        /*     * Dialog对话框提示用户删除操作     * position为删除图片位置     */    protected void dialog(final int position) {    	AlertDialog.Builder builder = new Builder(Property_repair.this);    	builder.setMessage("确认移除已添加图片吗?");    	builder.setTitle("提示");    	builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {    		@Override    		public void onClick(DialogInterface dialog, int which) {    			dialog.dismiss();    			imageItem.remove(position);    	        simpleAdapter.notifyDataSetChanged();    		}    	});    	builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {    		@Override    		public void onClick(DialogInterface dialog, int which) {    			dialog.dismiss();    			}    		});    	builder.create().show();    }    	    }



  相关解决方案