在 Android 上添加多个 onClick 和 onActivityResult 值时出现问题
Issues adding multiple onClick and onActivityResult values on Android
我正在尝试构建一个单页应用程序,该应用程序使用 ZXing
扫描条形码,然后允许我在便宜的蓝牙打印机上打印出 EditText
值。然后我可以清除 EditText
字段并重复该过程。我有打印和清除过程,但我无法将 ZXing
部分添加到主要 class 中。任何帮助或指导将不胜感激。忽略未显示的奇怪符号我对这些没有任何问题。谢谢
这是我的主要内容 class:
import com.zj.printdemo.R;
import android.app.Fragment;
import android.content.Intent;
import com.zj.btsdk.BluetoothService;
import com.zj.btsdk.PrintPic;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.view.View.OnClickListener;
import android.util.Log;
import android.view.ViewGroup;
public class PrintDemo extends Activity {
Button btnSearch;
Button btnSendDraw;
Button btnSend;
Button btnClose;
Button clearBtn;
Button btnBarcode;
EditText edtContext;
EditText edtContextQTY;
EditText edtPrint;
private static final int REQUEST_ENABLE_BT = 2;
BluetoothService mService = null;
BluetoothDevice con_dev = null;
private static final int REQUEST_CONNECT_DEVICE = 1; //��ȡ�豸��Ϣ
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//Button clearBtn = (Button)findViewById(R.id.btnClear);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mService = new BluetoothService(this, mHandler);
//�����������˳�����
if( mService.isAvailable() == false ){
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
}
clearBtn = (Button)findViewById(R.id.btnClear);
OnClickListener ClearOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
edtContext.setText("");
edtContextQTY.setText("");
}
};
clearBtn.setOnClickListener(ClearOnClickListener);
}
public void onStart() {
super.onStart();
//�����������
if( mService.isBTopen() == false)
{
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
try {
btnSendDraw = (Button) this.findViewById(R.id.btn_test);
btnSendDraw.setOnClickListener(new ClickEvent());
btnSearch = (Button) this.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new ClickEvent());
btnSend = (Button) this.findViewById(R.id.btnSend);
btnSend.setOnClickListener(new ClickEvent());
btnClose = (Button) this.findViewById(R.id.btnClose);
btnClose.setOnClickListener(new ClickEvent());
edtContext = (EditText) findViewById(R.id.txt_content);
edtContextQTY = (EditText) findViewById(R.id.txt_contentQTY);
btnClose.setEnabled(false);
btnSend.setEnabled(false);
btnSendDraw.setEnabled(false);
} catch (Exception ex) {
Log.e("������Ϣ",ex.getMessage());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mService != null)
mService.stop();
mService = null;
}
class ClickEvent implements View.OnClickListener {
public void onClick(View v) {
if (v == btnSearch) {
Intent serverIntent = new Intent(PrintDemo.this,DeviceListActivity.class); //��������һ����Ļ
startActivityForResult(serverIntent,REQUEST_CONNECT_DEVICE);
} else if (v == btnSend) {
String msg = edtContext.getText().toString();
String msgQTY = edtContextQTY.getText().toString();
if( msg.length() > 0 ){
mService.sendMessage("ItemID:"+msg+"\n","ItemQTY:"+msgQTY+"\n");
}
} else if (v == btnClose) {
mService.stop();
} else if (v == btnSendDraw) {
String msg = "";
String lang = getString(R.string.strLang);
//printImage();
byte[] cmd = new byte[3];
cmd[0] = 0x1b;
cmd[1] = 0x21;
if((lang.compareTo("en")) == 0){
cmd[2] |= 0x10;
mService.write(cmd); //��������ģʽ
mService.sendMessage("Congratulations!\n", "GBK");
cmd[2] &= 0xEF;
mService.write(cmd); //ȡ�����ߡ�����ģʽ
msg = " You have sucessfully created communications between your device and our bluetooth printer.\n\n"
+" the company is a high-tech enterprise which specializes" +
" in R&D,manufacturing,marketing of thermal printers and barcode scanners.\n\n";
mService.sendMessage(msg,"GBK");
}else if((lang.compareTo("ch")) == 0){
cmd[2] |= 0x10;
mService.write(cmd); //��������ģʽ
mService.sendMessage("��ϲ����\n", "GBK");
cmd[2] &= 0xEF;
mService.write(cmd); //ȡ�����ߡ�����ģʽ
msg = " ���Ѿ��ɹ��������������ǵ�������ӡ����\n\n"
+ " ����˾��һ��רҵ�����з�����������������Ʊ�ݴ�ӡ��������ɨ���豸��һ��ĸ߿Ƽ���ҵ.\n\n";
mService.sendMessage(msg,"GBK");
}
}
}
}
/**
* ����һ��Handlerʵ�������ڽ���BluetoothService��ػ�������Ϣ
*/
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BluetoothService.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothService.STATE_CONNECTED: //������
Toast.makeText(getApplicationContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
btnClose.setEnabled(true);
btnSend.setEnabled(true);
btnSendDraw.setEnabled(true);
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������","��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������","�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getApplicationContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
btnClose.setEnabled(false);
btnSend.setEnabled(false);
btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�������豸
Toast.makeText(getApplicationContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
}
//��ӡͼ��
@SuppressLint("SdCardPath")
private void printImage() {
byte[] sendData = null;
PrintPic pg = new PrintPic();
pg.initCanvas(384);
pg.initPaint();
pg.drawImage(0, 0, "/mnt/sdcard/icon.jpg");
sendData = pg.printDraw();
mService.write(sendData); //��ӡbyte������
}
这是我试图与上面结合的代码:
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
此外,这是我的页面主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="match_parent" android:id="@+id/MainParent">
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnSearch" >
</Button>
<EditText
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|top"
android:textSize="16sp"
android:hint="Item ID">
</EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt_contentQTY"
android:hint="Item QTY"
android:inputType="number"/>
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnSend" >
</Button>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_test" >
</Button>
<Button
android:id="@+id/btnClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnClose" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/button_barcode"
android:id="@+id/btnBarcode" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/button_clear"
android:id="@+id/btnClear" />
</LinearLayout>
问题:
当我添加 onClick 时,我必须在 OnCreate
中设置一个新的 OnClickListener
,当我这样做时,IntenIntegrator
在(这个)上有一个问题:IntentIntegrator scanIntegrator = new IntentIntegrator(this);
错误是 IntnentIntegrator
无法应用于(匿名 android.view、View.OnClickListener)
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
然后我不确定如何实现第二部分。我可以像这样组合 onActivityResult 吗:
public void onActivityResult(int requestCode, int resultCode, Intent data, Intent intent) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
4 月 14 日更新:
在@Tung 发布的帮助下,我能够解决 OnClickListener
问题。我做了一些调整,让应用程序编译并运行。
这是我的更新 OnClickListener
:
btnBarcode = (Button) findViewById(R.id.btnBarcode);
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
integrator.initiateScan();
}
}
};
btnBarcode.setOnClickListener(BarcodeOnClickListener);
新问题:
我现在有一个小问题,我认为这与我的 onActivityResult
有关。当我连接蓝牙打印机然后单击按钮扫描条形码时,它会断开我的蓝牙打印机。这是我的 onActivityResult
:
的代码
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
您的问题可能包含更多子问题。这就是为什么人们很难说出究竟是什么动作在帮助。
我会尝试从您的代码中阐明第一点。
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
this
导致
的错误
anonymous android.view,View.OnClickListener
要解决此问题,您必须将 this
替换为合适的参数(根据我在 GitHub 上搜索它的构造函数,我认为它应该是 Fragment/Activity)。所以它应该是(用一些 activity 替换 PrintDemo.this
包含代码):
IntentIntegrator scanIntegrator = new IntentIntegrator(PrintDemo.this);
试一试并更新您的问题。
Similar question
我不太明白问题,关于从按钮开始zxing扫描有答案。但是在我的应用程序中,我将蓝牙与 zxing 扫描仪一起使用,如果您能解决问题,我会提供帮助。所以:
<Button
android:id="@+id/btnFind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Find QR">
在主要活动中:
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
我正在尝试构建一个单页应用程序,该应用程序使用 ZXing
扫描条形码,然后允许我在便宜的蓝牙打印机上打印出 EditText
值。然后我可以清除 EditText
字段并重复该过程。我有打印和清除过程,但我无法将 ZXing
部分添加到主要 class 中。任何帮助或指导将不胜感激。忽略未显示的奇怪符号我对这些没有任何问题。谢谢
这是我的主要内容 class:
import com.zj.printdemo.R;
import android.app.Fragment;
import android.content.Intent;
import com.zj.btsdk.BluetoothService;
import com.zj.btsdk.PrintPic;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import android.view.View.OnClickListener;
import android.util.Log;
import android.view.ViewGroup;
public class PrintDemo extends Activity {
Button btnSearch;
Button btnSendDraw;
Button btnSend;
Button btnClose;
Button clearBtn;
Button btnBarcode;
EditText edtContext;
EditText edtContextQTY;
EditText edtPrint;
private static final int REQUEST_ENABLE_BT = 2;
BluetoothService mService = null;
BluetoothDevice con_dev = null;
private static final int REQUEST_CONNECT_DEVICE = 1; //��ȡ�豸��Ϣ
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//Button clearBtn = (Button)findViewById(R.id.btnClear);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mService = new BluetoothService(this, mHandler);
//�����������˳�����
if( mService.isAvailable() == false ){
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
}
clearBtn = (Button)findViewById(R.id.btnClear);
OnClickListener ClearOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
edtContext.setText("");
edtContextQTY.setText("");
}
};
clearBtn.setOnClickListener(ClearOnClickListener);
}
public void onStart() {
super.onStart();
//�����������
if( mService.isBTopen() == false)
{
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
try {
btnSendDraw = (Button) this.findViewById(R.id.btn_test);
btnSendDraw.setOnClickListener(new ClickEvent());
btnSearch = (Button) this.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new ClickEvent());
btnSend = (Button) this.findViewById(R.id.btnSend);
btnSend.setOnClickListener(new ClickEvent());
btnClose = (Button) this.findViewById(R.id.btnClose);
btnClose.setOnClickListener(new ClickEvent());
edtContext = (EditText) findViewById(R.id.txt_content);
edtContextQTY = (EditText) findViewById(R.id.txt_contentQTY);
btnClose.setEnabled(false);
btnSend.setEnabled(false);
btnSendDraw.setEnabled(false);
} catch (Exception ex) {
Log.e("������Ϣ",ex.getMessage());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mService != null)
mService.stop();
mService = null;
}
class ClickEvent implements View.OnClickListener {
public void onClick(View v) {
if (v == btnSearch) {
Intent serverIntent = new Intent(PrintDemo.this,DeviceListActivity.class); //��������һ����Ļ
startActivityForResult(serverIntent,REQUEST_CONNECT_DEVICE);
} else if (v == btnSend) {
String msg = edtContext.getText().toString();
String msgQTY = edtContextQTY.getText().toString();
if( msg.length() > 0 ){
mService.sendMessage("ItemID:"+msg+"\n","ItemQTY:"+msgQTY+"\n");
}
} else if (v == btnClose) {
mService.stop();
} else if (v == btnSendDraw) {
String msg = "";
String lang = getString(R.string.strLang);
//printImage();
byte[] cmd = new byte[3];
cmd[0] = 0x1b;
cmd[1] = 0x21;
if((lang.compareTo("en")) == 0){
cmd[2] |= 0x10;
mService.write(cmd); //��������ģʽ
mService.sendMessage("Congratulations!\n", "GBK");
cmd[2] &= 0xEF;
mService.write(cmd); //ȡ�����ߡ�����ģʽ
msg = " You have sucessfully created communications between your device and our bluetooth printer.\n\n"
+" the company is a high-tech enterprise which specializes" +
" in R&D,manufacturing,marketing of thermal printers and barcode scanners.\n\n";
mService.sendMessage(msg,"GBK");
}else if((lang.compareTo("ch")) == 0){
cmd[2] |= 0x10;
mService.write(cmd); //��������ģʽ
mService.sendMessage("��ϲ����\n", "GBK");
cmd[2] &= 0xEF;
mService.write(cmd); //ȡ�����ߡ�����ģʽ
msg = " ���Ѿ��ɹ��������������ǵ�������ӡ����\n\n"
+ " ����˾��һ��רҵ�����з�����������������Ʊ�ݴ�ӡ��������ɨ���豸��һ��ĸ߿Ƽ���ҵ.\n\n";
mService.sendMessage(msg,"GBK");
}
}
}
}
/**
* ����һ��Handlerʵ�������ڽ���BluetoothService��ػ�������Ϣ
*/
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BluetoothService.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothService.STATE_CONNECTED: //������
Toast.makeText(getApplicationContext(), "Connect successful",
Toast.LENGTH_SHORT).show();
btnClose.setEnabled(true);
btnSend.setEnabled(true);
btnSendDraw.setEnabled(true);
break;
case BluetoothService.STATE_CONNECTING: //��������
Log.d("��������","��������.....");
break;
case BluetoothService.STATE_LISTEN: //�������ӵĵ���
case BluetoothService.STATE_NONE:
Log.d("��������","�ȴ�����.....");
break;
}
break;
case BluetoothService.MESSAGE_CONNECTION_LOST: //�����ѶϿ�����
Toast.makeText(getApplicationContext(), "Device connection was lost",
Toast.LENGTH_SHORT).show();
btnClose.setEnabled(false);
btnSend.setEnabled(false);
btnSendDraw.setEnabled(false);
break;
case BluetoothService.MESSAGE_UNABLE_CONNECT: //�������豸
Toast.makeText(getApplicationContext(), "Unable to connect device",
Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
}
//��ӡͼ��
@SuppressLint("SdCardPath")
private void printImage() {
byte[] sendData = null;
PrintPic pg = new PrintPic();
pg.initCanvas(384);
pg.initPaint();
pg.drawImage(0, 0, "/mnt/sdcard/icon.jpg");
sendData = pg.printDraw();
mService.write(sendData); //��ӡbyte������
}
这是我试图与上面结合的代码:
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
contentTxt.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
此外,这是我的页面主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="match_parent" android:id="@+id/MainParent">
<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnSearch" >
</Button>
<EditText
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|top"
android:textSize="16sp"
android:hint="Item ID">
</EditText>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txt_contentQTY"
android:hint="Item QTY"
android:inputType="number"/>
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnSend" >
</Button>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_test" >
</Button>
<Button
android:id="@+id/btnClose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnClose" >
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/button_barcode"
android:id="@+id/btnBarcode" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/button_clear"
android:id="@+id/btnClear" />
</LinearLayout>
问题:
当我添加 onClick 时,我必须在 OnCreate
中设置一个新的 OnClickListener
,当我这样做时,IntenIntegrator
在(这个)上有一个问题:IntentIntegrator scanIntegrator = new IntentIntegrator(this);
错误是 IntnentIntegrator
无法应用于(匿名 android.view、View.OnClickListener)
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
}
然后我不确定如何实现第二部分。我可以像这样组合 onActivityResult 吗:
public void onActivityResult(int requestCode, int resultCode, Intent data, Intent intent) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
4 月 14 日更新:
在@Tung 发布的帮助下,我能够解决 OnClickListener
问题。我做了一些调整,让应用程序编译并运行。
这是我的更新 OnClickListener
:
btnBarcode = (Button) findViewById(R.id.btnBarcode);
OnClickListener BarcodeOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
integrator.initiateScan();
}
}
};
btnBarcode.setOnClickListener(BarcodeOnClickListener);
新问题:
我现在有一个小问题,我认为这与我的 onActivityResult
有关。当我连接蓝牙打印机然后单击按钮扫描条形码时,它会断开我的蓝牙打印机。这是我的 onActivityResult
:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT: //���������
if (resultCode == Activity.RESULT_OK) { //�����Ѿ���
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else { //�û������������
finish();
}
break;
case REQUEST_CONNECT_DEVICE: //��������ijһ�����豸
if (resultCode == Activity.RESULT_OK) { //�ѵ�������б��е�ij���豸��
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS); //��ȡ�б������豸��mac��ַ
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
您的问题可能包含更多子问题。这就是为什么人们很难说出究竟是什么动作在帮助。
我会尝试从您的代码中阐明第一点。
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
this
导致
anonymous android.view,View.OnClickListener
要解决此问题,您必须将 this
替换为合适的参数(根据我在 GitHub 上搜索它的构造函数,我认为它应该是 Fragment/Activity)。所以它应该是(用一些 activity 替换 PrintDemo.this
包含代码):
IntentIntegrator scanIntegrator = new IntentIntegrator(PrintDemo.this);
试一试并更新您的问题。
Similar question
我不太明白问题,关于从按钮开始zxing扫描有答案。但是在我的应用程序中,我将蓝牙与 zxing 扫描仪一起使用,如果您能解决问题,我会提供帮助。所以:
<Button
android:id="@+id/btnFind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Find QR">
在主要活动中:
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}