您好,登錄后才能下訂單哦!
附完整代碼,此代碼只實現功能,美觀及其它不考慮(*^__^*)
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。