package com.amani.main;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class chooser extends Activity {
private ImageView imageView;
private OnClickListener imgViewListener;
Bitmap myBitmap;
private byte[] mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.ivPic);
imgViewListener = new OnClickListener() {
public void onClick(View v) {
final CharSequence[] items = { "相册", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(chooser.this).setTitle("选择图片").setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int item) {
//这里item是根据选择的方式, 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
if(item==1){
Intent getImageByCamera= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, 1);
}else{
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}
}
}).create();
dlg.show();
}
};
//给imageView控件绑定点点击监听器
imageView.setOnClickListener(imgViewListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver resolver = getContentResolver();
/**
* 因为两种方式都用到了startActivityForResult方法,这个方法执行完后都会执行onActivityResult方法,
* 所以为了区别到底选择了那个方式获取图片要进行判断,这里的requestCode跟startActivityForResult里面第二个参数对应
*/
if (requestCode == 0) {
try {
//获得图片的uri
Uri originalUri = data.getData();
//将图片内容解析成字节数组
mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
//将字节数组转换为ImageView可调用的Bitmap对象
myBitmap = getPicFromBytes(mContent, null);
////把得到的图片绑定在控件上显示
imageView.setImageBitmap(myBitmap);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}else if(requestCode ==1){
try {
super.onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
myBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos);
mContent=baos.toByteArray();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把得到的图片绑定在控件上显示
imageView.setImageBitmap(myBitmap);
}
}
public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) {
if (bytes != null)
if (opts != null)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts);
else
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return null;
}
public static byte[] readStream(InputStream inStream) throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inStream.close();
return data;
}
}
1.用来保存对象
private List<View> listOfPhoneSubView;//存放子View对象 private List<Button> listOfPhoneButton;//左边的按钮,用来选择类型,文本的获取也是通过这里 private List<EditText> listOfPhoneEditText;//右边的文本框,用来获取电话号码 private List<String> listOfPhoneString;//用来保存左边按钮被点击的时候需要传递到别的Activity并显示的字符 private List<Object> listOfPhoneTag;//用来标记对象 private static List<HashMap<String, String>> listOfResolvedPhone;//存放从系统中解析出来的电话信息 private int tagFlag = 0;
2.初始化
private void initList()//初始化ArrayList { listOfPhoneSubView = new ArrayList<View>(); listOfPhoneButton = new ArrayList<Button>(); listOfPhoneEditText = new ArrayList<EditText>(); listOfPhoneString = new ArrayList<String>(); listOfPhoneTag = new ArrayList<Object>(); } private final String string1 = "住宅";//这个不要这样做,可以去读取系统的 private final String string2 = "手机"; private final String string3 = "单位"; private final String string4 = "单位传真"; private final String string5 = "住宅传真"; private final String string6 = "寻呼机"; private final String string7 = "其他"; private final String string8 = "回拨号码"; private final String string9 = "车载电话"; private final String string10 = "公司总机"; private final String string11 = "ISDN"; private final String string12 = "总机"; private final String string13 = "其他传真"; private final String string14 = "无线装置"; private final String string15 = "电报"; private final String string16 = "TTY TDD"; private final String string17 = "单位手机"; private final String string18 = "单位寻呼机"; private final String string19 = "助理"; private final String string20 = "彩信"; private final String string21 = "自定义"; public void initStringListOfNumber()//初始化listOfPhoneString { listOfPhoneString.add(string1); listOfPhoneString.add(string2); listOfPhoneString.add(string3); listOfPhoneString.add(string4); listOfPhoneString.add(string5); listOfPhoneString.add(string6); listOfPhoneString.add(string7); listOfPhoneString.add(string8); listOfPhoneString.add(string9); listOfPhoneString.add(string10); listOfPhoneString.add(string11); listOfPhoneString.add(string12); listOfPhoneString.add(string13); listOfPhoneString.add(string14); listOfPhoneString.add(string15); listOfPhoneString.add(string16); listOfPhoneString.add(string17); listOfPhoneString.add(string18); listOfPhoneString.add(string19); listOfPhoneString.add(string20); listOfPhoneString.add(string21); }
3.开启线程到系统中获取数据
Runnable r = new Runnable() { //处理数据 public void run() { listOfResolvedPhone = QueryLinkmanInfoByRawContactId.queryPhoneInfo(EditLinkman.this, rawContactId); Message msg = new Message(); myHandler.sendMessage(msg);//得到电话信息以后通知更新界面 } };
在queryPhoneInfo()中,我们调用并查询了系统的信息,并对系统中的数据进行匹配处理,最终返回回来得到listOfResolvedPhone会是List<HashMap<String, String>>保存的用户的信息,比如:PHONE_TYPE_ID:1
PHONE_TYPE:住宅 PHONE_NUMBER:13023895363
4.更新界面
private Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { showPhone();//根据listOfResolvedPhone中的值初始化显示电话信息的LinearLayout->llPhone } }; public void showPhone() { //让顶部啊按钮监听事件 btnPhone.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 增加一个子View System.out.println("增加一个Phone子View"); addSubViewOfPhone(); } }); if(listOfResolvedPhone.isEmpty()) { System.out.println("PhoneList is empty"); //do nothing... } else { System.out.println("PhoneList is NO empty"); int sizeOfResolvedPhone = listOfResolvedPhone.size(); for(int i = 0; i < sizeOfResolvedPhone; i++) { // String phoneTypeId = listOfResolvedPhone.get(i).get(LD_PHONE_ID);//这样做虽然比较好,但是逻辑太乱了,暂时不考虑 String phoneType = listOfResolvedPhone.get(i).get(LD_PHONE_TYPE); String phoneNumber = listOfResolvedPhone.get(i).get(LD_PHONE); LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View subView = inflater.inflate(R.layout.sub_view_of_add_linkman, null); Button btnChooseType = (Button)subView.findViewById(R.id.svoal_btn_choose_type); EditText rlEditText = (EditText)subView.findViewById(R.id.svoal_et_input); rlEditText.setInputType(InputType.TYPE_CLASS_NUMBER);//设置输入类型和键盘为数字 RelativeLayout rlDel = (RelativeLayout)subView.findViewById(R.id.svoal_rl_del); System.out.println("phoneType:" + phoneType); System.out.println("phoneNumber:" + phoneNumber); btnChooseType.setText(phoneType); rlEditText.setText(phoneNumber); btnChooseType.setOnClickListener(new BtnChooseNumberTypeClickedListener()); rlDel.setOnClickListener(new RlNumberDelClickedListener()); subView.setTag(tagFlag); btnChooseType.setTag(tagFlag); rlEditText.setTag(tagFlag); rlDel.setTag(tagFlag); listOfPhoneSubView.add(subView); listOfPhoneButton.add(btnChooseType); listOfPhoneEditText.add(rlEditText); listOfPhoneTag.add(rlDel.getTag()); tagFlag ++; llPhone.addView(subView, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);//增加一个View } } }
tag的作用是什么呢?
为什么要给每一个控件都加上一个Tag呢?
有两个方面:
1.当用户删除某一个元素的时候,不一定是从哪里删除,有可能是从列表中间删除,所以当用户点击子View上面的删除按钮的时候,需要通过v.getTag()获得要被删除的是哪个元素。通过这个Tag就可以将他们都删除了。
2.当点击选择电话类型的时候,同样,你需要把用户选择的哪个类型的字符串返回回来,你如何锁定是哪个Button需要被更改呢?同样也是通过Tag,我们在打开选择类型的Activity的时候,不仅将让用户进行选择的List传递过去,也将Tag传递过去,传递过去以后什么都不做,直接返回,返回回来以后就可以找到是哪个Button上面的类型名需要更改了。
public void addSubViewOfPhone() { LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View subView = inflater.inflate(R.layout.sub_view_of_add_linkman, null); Button btnChooseType = (Button)subView.findViewById(R.id.svoal_btn_choose_type);//选择号码类型 EditText rlEditText = (EditText)subView.findViewById(R.id.svoal_et_input);//接收号码的输入 rlEditText.setHint("电话"); rlEditText.setInputType(InputType.TYPE_CLASS_NUMBER);//设置输入类型和键盘为数字 RelativeLayout rlDel = (RelativeLayout)subView.findViewById(R.id.svoal_rl_del);//右边的删除按钮 btnChooseType.setOnClickListener(new BtnChoosePhoneTypeClickedListener()); rlDel.setOnClickListener(new RlNumberDelClickedListener()); //在创建的时候判断下现在是第几个了,设置btnChooseType上默认显示的内容 switch(listOfPhoneTag.size()) { case 0: btnChooseType.setText(string1); break; case 1: btnChooseType.setText(string2); break; case 2: btnChooseType.setText(string3); break; case 3: btnChooseType.setText(string4); break; case 4: btnChooseType.setText(string5); break; case 5: btnChooseType.setText(string6); break; case 6: btnChooseType.setText(string7); break; case 7: btnChooseType.setText(string8); break; default: btnChooseType.setText(string8);//其他的暂时不考虑啦。就取前8吧 } subView.setTag(tagFlagOfPhone); btnChooseType.setTag(tagFlagOfPhone); rlEditText.setTag(tagFlagOfPhone); rlDel.setTag(tagFlagOfPhone); listOfPhoneSubView.add(subView); listOfPhoneButton.add(btnChooseType); listOfPhoneEditText.add(rlEditText); listOfPhoneTag.add(rlDel.getTag()); tagFlagOfPhone ++; llPhone.addView(subView, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); } /** * 删除一个电话subView * */ class RlNumberDelClickedListener implements OnClickListener { public void onClick(View v) { System.out.println("删除:"); //根据tag找到它在list中的位置,删除中所有list中对应的 int id = -1; int sizeOfTag = listOfPhoneTag.size(); for(int i = 0; i < sizeOfTag; i++) { if((Integer)listOfPhoneTag.get(i) == (Integer)(v.getTag())) { System.out.println("找到并删除了索引:" + i); id = i; llPhone.removeView(listOfPhoneSubView.get(id));//越界了 listOfPhoneSubView.remove(id); listOfPhoneButton.remove(id); listOfPhoneEditText.remove(id); listOfPhoneTag.remove(id);//记得Remove掉自己啊!!! break; } else { System.out.println("不可能找不到的!"); } } } } /** * 选择号码类型的按钮被点击的时候 * */ private int tagFlagOfPhone = 0; class BtnChoosePhoneTypeClickedListener implements OnClickListener { public void onClick(View v) { System.out.println("BtnChooseTypeClickedListener:"); System.out.println("v.getTag():" + (Integer)v.getTag()); Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt(TAG, (Integer)v.getTag());//这个传递到DialogActivity再从那边传递过来,就可以知道是谁传递的了 bundle.putString(TITLE, "请选择号码类型"); bundle.putString(FLAG, NUMBER); if(listOfPhoneString.isEmpty()) { initStringListOfNumber(); bundle.putStringArrayList(STRING_LIST, (ArrayList<String>)listOfPhoneString); } else { bundle.putStringArrayList(STRING_LIST, (ArrayList<String>)listOfPhoneString); } intent.putExtras(bundle); intent.setClass(EditLinkman.this, DialogActivity.class); startActivityForResult(intent, REQUEST_CODE); } } /** * 将TAG的值传递进来,到TAG数组中进行值匹配 * 将匹配到的索引返回 * */ public int getIndexOfPhoneTag(int TAG) { int index = -1; int sizeOfTag = listOfPhoneTag.size(); for(int i = 0; i < sizeOfTag; i++) { if((Integer)listOfPhoneTag.get(i) == TAG) { index = i; } } return index; }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); System.out.println("EditLinkman--onActivityResult"); if(requestCode == REQUEST_CODE) { if(resultCode == RESULT_OK) { System.out.println("EditLinkman--RESULT_OK"); Bundle extras = data.getExtras(); if (extras != null) { System.out.println("extras != null:FLAG:" + extras.getString(FLAG).toString()); if(extras.getInt(TAG) == -1) { System.out.println("收到传递回来的空TAG" + extras.getString("TAG")); } else if(extras.getString(FLAG).equals(NUMBER)) { System.out.println("收到传递回来的值" + extras.getInt(TAG)); listOfPhoneButton.get(getIndexOfPhoneTag(extras.getInt(TAG))).setText(extras.getString(VALUE)); } } } } }
java代码中通过btn1关联次控件
android:id="@+id/btn1"
控件宽度
android:layout_width="80px" //"80dip"或"80dp"
android:layout_width =“wrap_content”
android:layout_width =“match_parent”
控件高度
android:layout_height="80px" //"80dip"或"80dp"
android:layout_height =“wrap_content”
android:layout_height =“match_parent”
控件排布
android:orientation="horizontal”
android:orientation="vertical“
控件间距
android:layout_marginLeft="5dip" //距离左边
android:layout_marginRight="5dip" //距离右边
android:layout_marginTop="5dip" //距离上面
android:layout_marginRight="5dip" //距离下面
控件显示位置
android:gravity="center" //left,right, top, bottom
android:gravity="center_horizontal"
android:layout_gravity是本元素对父元素的重力方向。
android:layout_gravity属性则设置控件本身相对于父控件的显示位置
android:gravity是本元素所有子元素的重力方向。
android:layout_gravity="center_vertical"
android:layout_gravity="left"
android:layout_gravity="left|bottom"
TextView中文本字体
android:text="@String/text1" //在string.xml中定义text1的值
android:textSize="20sp"
android:textColor=”#ff123456”
android:text //普通(normal), 斜体(italic),粗斜体(bold_italic)
定义控件是否可见
android:visibility=”visible” //可见
android:visibility=”invisible” //不可见,但是在布局中占用的位置还在
android:visibility=”gone” //不可见,完全从布局中消失
定义背景图片
android:background="@drawable/img_bg" //img_bg为drawable下的一张图片
seekbar控件背景图片及最大值
android:progressDrawable="@drawable/seekbar_img"
android:thumb="@drawable/thumb"
android:max = "60"