布局文件实现
<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" >
<Button
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="扫描二维码"
android:textSize="20sp" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="扫描结果"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="2dip"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:background="@android:color/holo_blue_bright" />
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入目标信息生成对应二维码" />
<Button
android:id="@+id/made"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生成二维码"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
后台代码操作
/**
* 在实现之前需要先关联BarCodeTest项目
*/
private Button search;
private Button made;
private TextView result;
private EditText msg;
private ImageView imageCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
search = (Button) findViewById(R.id.search);
made = (Button) findViewById(R.id.made);
result = (TextView) findViewById(R.id.result);
msg = (EditText) findViewById(R.id.msg);
imageCode = (ImageView) findViewById(R.id.imageCode);
}
private void initData() {
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, 0);
}
});
made.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String info = msg.getText().toString();
if (info != null) {
try {
Bitmap bitmap = EncodingHandler.createQRCode(info, 600);
imageCode.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
String info = data.getStringExtra("result");
result.setText(info);
}
}