您好,登录后才能下订单哦!
附完整代码,此代码只实现功能,美观及其它不考虑(*^__^*)
java代码:
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView iv_img;
private TextView tv_remark;
private AlertDialog dialog;
private EditText et_remark;
private List<Bitmap> lb = new ArrayList<Bitmap>();
private List<String> ls = new ArrayList<String>();
private GridView gv;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
gv = (GridView) findViewById(R.id.gv_show);
}
private void initData() {
adapter = new MyAdapter();
gv.setAdapter(adapter);
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return lb.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(getApplicationContext(), R.layout.gv_item,
null);
iv_img = (ImageView) view.findViewById(R.id.iv_img);
tv_remark = (TextView) view.findViewById(R.id.tv_remark);
Bitmap bm = lb.get(position);
String remark = ls.get(position);
iv_img.setImageBitmap(bm);
tv_remark.setText(remark);
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_paizhao:
onRemark();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
// 拍照Activity保存图像数据的key是data,返回的数据类型是Bitmap对象
Bitmap cameraBitmap = data.getExtras().getParcelable("data");
// 将拍到的照片存放于集合中
lb.add(cameraBitmap);
initData();
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 给照片添加备注信息
*/
public void onRemark() {
AlertDialog.Builder builder = new Builder(MainActivity.this);
dialog = builder.create();
View view = View
.inflate(getApplicationContext(), R.layout.dialog_remark, null);
et_remark = (EditText) view.findViewById(R.id.et_remark);
Button bt_ok = (Button) view.findViewById(R.id.bt_ok);
Button bt_no = (Button) view.findViewById(R.id.bt_no);
bt_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String remark = et_remark.getText().toString().trim();
if (TextUtils.isEmpty(remark)) {
Toast.makeText(getApplicationContext(), "添点备注吧~~~⊙﹏⊙‖∣",
2000).show();
} else {
// 将备注信息添加到集合中
ls.add(remark);
// 调用系统照相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
dialog.dismiss();
}
}
});
bt_no.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setView(view, 0, 0, 0, 0);
dialog.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:orientation="vertical" >
<GridView
android:id="@+id/gv_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="2" />
</LinearLayout>
dialog_remark.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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/beizhu"
android:textColor="@android:color/black"
android:textSize="18dip" />
<EditText
android:id="@+id/et_remark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:textColor="@android:color/black" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/bt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dip"
android:text="@string/ok" />
<Button
android:id="@+id/bt_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dip"
android:text="@string/no" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
gv_item.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:orientation="vertical" >
<ImageView
android:id="@+id/iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_remark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
menu文件下的main.xml布局:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_paizhao"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:icon="@android:drawable/ic_menu_add"/>
</menu>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。