标签:
1 package com.example.ewm; 2 3 import com.google.zxing.WriterException; 4 import com.zxing.activity.CaptureActivity; 5 import com.zxing.encoding.EncodingHandler; 6 7 import android.os.Bundle; 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.graphics.Bitmap; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 import android.widget.EditText; 15 import android.widget.ImageView; 16 import android.widget.TextView; 17 import android.widget.Toast; 18 19 public class MainActivity extends Activity { 20 private Button scanButton,genButton; 21 private TextView textView; 22 private EditText editText; 23 private ImageView imageView; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 scanButton = (Button) findViewById(R.id.scan); 29 genButton = (Button) findViewById(R.id.button1); 30 editText = (EditText) findViewById(R.id.editText1); 31 imageView = (ImageView) findViewById(R.id.imageView1); 32 textView = (TextView) findViewById(R.id.textView1); 33 //扫描 34 scanButton.setOnClickListener(new OnClickListener() { 35 36 @Override 37 public void onClick(View v) { 38 // TODO Auto-generated method stub 39 Toast.makeText(MainActivity.this, "你可以扫描条形码或者二维码", 100).show(); 40 //captureactivity是在类库里面的一个页面, 41 Intent intent = new Intent(MainActivity.this, CaptureActivity.class); 42 //这个跳转方式是能 获得一个返回值。配合下面的onActivityResult方法一起使用的 43 startActivityForResult(intent, 0); 44 } 45 }); 46 //生成 47 genButton.setOnClickListener(new OnClickListener() { 48 49 @Override 50 public void onClick(View v) { 51 // TODO Auto-generated method stub 52 //获得EditText里面的String值 53 String in = editText.getText().toString(); 54 //如果获得值为空,提示输入文本 55 if(in.equals("")){ 56 Toast.makeText(MainActivity.this, "请输入文本", Toast.LENGTH_SHORT).show(); 57 //否则, 58 }else { 59 try { 60 //调用类库里面的EncodingHandler这个方法来将字符串转换成二维码 61 Bitmap qrcode = EncodingHandler.createQRCode(in, 300); 62 //设置成Imageview的图片 63 imageView.setImageBitmap(qrcode); 64 } catch (WriterException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 } 69 } 70 }); 71 } 72 73 @Override 74 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 75 // TODO Auto-generated method stub 76 super.onActivityResult(requestCode, resultCode, data); 77 78 if(resultCode == RESULT_OK){ 79 //获得Intent返回值里面一个KEY是result的String值。 80 String result = data.getExtras().getString("result"); 81 //加到Textview里面 82 textView.setText(result); 83 } 84 } 85 86 }
标签:
原文地址:http://www.cnblogs.com/pastor/p/5156705.html