我有两个按钮,它们都有条形码扫描。一旦它从扫描返回,我想知道用户点击了哪个按钮,这样我可以采取不同的路径" onActivityResult"方法。我怎么做?
我可以找出在onClick()方法中点击了哪个按钮但是没有帮助。
public class ReaderActivity extends AppCompatActivity {
private Button scan_btn, verifyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
verifyButton = (Button) findViewById(R.id.buttonVerify);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
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){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, result.getContents(),Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
答案 0 :(得分:0)
您可以在活动中添加布尔值
例如:
private boolean isScanButtonClicked;
使用以下代码
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isScanButtonClicked = true; // true a scan button clicked
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isScanButtonClicked = false; // false as scan button not clicked
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
根据 isScanButtonClicked 值,您可以在onActivityResult()中执行操作
希望这些可以帮助你。
答案 1 :(得分:0)
您可以向 Intent data 添加一些标记/数据并将此数据处理为 onActivityResult ,以便在您的操作中点击该按钮,或更改 resultCode 以正确的方式处理此结果。
答案 2 :(得分:0)
public class MainActivity extends AppCompatActivity {
public Button btnOne;
public Button btnTwo;
public String btnClicked;
public static int VALUEONE = 1;
public static int VALUETWO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnUm = (Button)findViewById(R.id.btnUm);
btnDois = (Button)findViewById(R.id.btnDois);
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent test = new Intent(MainActivity.this, NextActivity.class);
btnClicked = "btnOne";
startActivityForResult(test,VALUEONE);
}
});
btnTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent test = new Intent(MainActivity.this, NextActivity.class);
btnClicked = "btnTwo";
startActivityForResult(test,VALUETWO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("Btn clicked: " + btnClicked);
}
...........
我在Android Studio中尝试使用此代码并且可以正常运行。你做了类似的事情吗?
答案 3 :(得分:0)
通过get set方法解决它
创建了如下所示的类
public class myApplication extends Application{
private String someVariable;
public String getWhichButton() {
return someVariable;
}
public void setWhichButton(String someVariable) {
this.someVariable = someVariable;
}
}
请务必在AndroidManifest的android:name=".myApplication"
标记中添加<Application>
。然后在按钮点击操作期间的ReaderActivity中,我设置了
((myApplication) activity.getApplication()).setWhichButton("scan");
OR
((myApplication) activity.getApplication()).setWhichButton("Verify");
取决于我点击的按钮。在onActivityResult方法中我使用
((myApplication) this.getApplication()).getWhichButton();
检索单击哪个按钮以相应地执行操作。
public class ReaderActivity extends AppCompatActivity{
private Button scan_btn, verifyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
scan_btn = (Button) findViewById(R.id.scan_btn);
verifyButton = (Button) findViewById(R.id.buttonVerify);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("scan");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
verifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((myApplication) activity.getApplication()).setWhichButton("verify");
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("LINE UP BARCODE WITH SCANNER");
integrator.setCameraId(0);
integrator.setBeepEnabled(true);
integrator.setBarcodeImageEnabled(false);
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){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, result.getContents(),Toast.LENGTH_LONG).show();
String val = ((myApplication) this.getApplication()).getWhichButton();
if(val=="scan") {
Toast.makeText(this, "Scan button pressed", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(this, "Verify button pressed", Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}