我正在制作AlertDialog,该对话框用于将项目添加到我的收货清单中。在AlertDialog中,有一个按钮可以访问Zxing QR Scanner。我想要的是在系统获得QR扫描结果之后,将其返回给alertdialog。我已经在QR扫描活动中进行了编码以使该活动意图返回,但是问题是当其意图返回时,它不会自动再次打开“警报对话框”。
这是从QR活动获得结果的代码行,我应该在此处添加什么以自动打开警报对话框?
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
qrResult = null;
} else {
qrResult = extras.getString("QRItemtype");
}
这就是我从QR扫描活动中导航回来的方式
Intent qrResult = new Intent(QRScannerActivity.this, CreateReceiptActivity.class);
itemTypeQR = result.getContents();
qrResult.putExtra("QRItemtype",itemTypeQR);
startActivity(qrResult);
这是完整的代码:
public class CreateReceiptActivity extends AppCompatActivity {
@BindView(R.id.receipt_date)
TextView date;
@BindView(R.id.receipt_invoice)
TextView invoiceNumber;
@BindView(R.id.btn_receipt_add_item)
ImageButton addItem;
@BindView(R.id.btn_receipt_print)
ImageButton printItem;
@BindView(R.id.receipt_view_recycler)
RecyclerView recyclerView;
@BindView(R.id.create_receipt_pb_loading)
ProgressBar pbloading;
List<ListAutoComplete> autoCompleteList;
ListAutoComplete listAutoComplete;
List<ListReceiptItem> receiptItemList;
ListReceiptItem listReceiptItem;
ReceiptItemAdapter adapter;
public String itemType, itemQty, itemPrice, itemDate, itemInvoice, lastInvoice, qrResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_receipt);
ButterKnife.bind(this);
receiptItemList = new ArrayList<>();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ReceiptItemAdapter(this, receiptItemList);
recyclerView.setAdapter(adapter);
itemInvoice = invoiceNumber.getText().toString();
itemDate = setDate(date);
date.setText(this.getString(R.string.date, setDate(date)));
printItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
pbloading.setVisibility(View.VISIBLE);
cutStock();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
builder.setMessage("Print Transaksi ?").setPositiveButton("Ya", dialogClickListener)
.setNegativeButton("Tidak", dialogClickListener).show();
}
});
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
qrResult = null;
} else {
qrResult = extras.getString("QRItemtype");
}
} else {
qrResult = (String) savedInstanceState.getSerializable("QRItemtype");
}
addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// getTypeList();
LayoutInflater li = CreateReceiptActivity.this.getLayoutInflater();
final View v = li.inflate(R.layout.alertdialog_create_receipt, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
builder.setView(v);
final EditText addItemType = v.findViewById(R.id.alertdialog_receipt_type);
final EditText addItemQty = v.findViewById(R.id.alertdialog_receipt_qty);
final EditText addItemPrice = v.findViewById(R.id.alertdialog_receipt_price);
Button btnSubmit = v.findViewById(R.id.alertdialog_receipt_submit);
Button btnScan = v.findViewById(R.id.alertdialog_receipt_scanqr);
if (qrResult == null){
Toast.makeText(CreateReceiptActivity.this, "Gagal scan. Masukkan Tipe Secara Manual", Toast.LENGTH_SHORT).show();
}
else {
addItemType.setText(qrResult);
}
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
startActivity(i);
}
});
final AlertDialog alertDialog = builder.show();
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemType = addItemType.getText().toString().trim();
itemQty = addItemQty.getText().toString().trim();
itemPrice = addItemPrice.getText().toString().trim();
listReceiptItem = new ListReceiptItem(itemType, itemQty, itemPrice, "0");
receiptItemList.add(listReceiptItem);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
alertDialog.dismiss();
Toast.makeText(CreateReceiptActivity.this, "barang tertambah", Toast.LENGTH_SHORT).show();
}
});
}
});
}
答案 0 :(得分:0)
CreateReceiptActivity
实例:在CreateReceiptActivity
中:
private static final int QR_REQUEST_CODE = 1;
...
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
startActivityForResult(i, QR_REQUEST_CODE);
}
});
在QRScannerActivity
中:
Intent qrResult = new Intent();
itemTypeQR = result.getContents();
qrResult.putExtra("QRItemtype",itemTypeQR);
setResult(RESULT_OK, intent);
finish();
然后在CreateReceiptActivity
中输入
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {return;}
qrResult = data.getString("QRItemtype");
// do you want with scanned QR
}