我正在尝试在Zxing Scanner页面中实现按钮。我尝试了这个链接Add the flashlight button above the Zxing Scanner page,但它在我的代码中无效。这里我附上了java代码。请提出一些好的解决方案。
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button;
import android.widget.FrameLayout;
import com.google.zxing.Result;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Toast;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class WorkOut extends AppCompatActivity implements ZXingScannerView.ResultHandler{
Integer response = 0 ;
int currentIndex=0;
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this);
Button scanButton = new Button(this);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
scanButton.setText("Flash Light");
scanButton.setLayoutParams(params);
setContentView(mScannerView);
mScannerView.addView(scanButton);
}
private void checkPermissions() {
try {
for (int i = currentIndex; i < permissions.length; i++) {
currentIndex = currentIndex + 1;
int result = ContextCompat.checkSelfPermission(this, permissions[i]);
if (result == PackageManager.PERMISSION_GRANTED) {
} else {
requestPermission(permissions[i]);
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Activity activity = this;
Context context = this;
String[] permissions = new String[]{Manifest.permission.CAMERA};
private void requestPermission(String permission) {
ActivityCompat.requestPermissions(activity, new String[]{permission},
101);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
checkPermissions();
} else {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
@Override
public void onResume() {
super.onResume();
if(response == 1) {
mScannerView = null;
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
response = 0;
}
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void onDestroy() {
super.onDestroy();
mScannerView.stopCamera();
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("ani","onrestart");
}
@Override
public void handleResult(Result rawResult) {
Toast.makeText(getApplicationContext(),"Scanned",Toast.LENGTH_SHORT).show();
}
}