在我的Fragment
中将扫描的字符串添加到EditText的最佳方法是什么?
我尝试使用bundles
,但因为我想在现有Fragment
中收到字符串,有人说最好搜索其他方式。 Atm我的onClickListener
中有Fragment
我在scanSerial
中调用MainActivity
方法。在我的MainActivity
中,还有onActivityResult
inputSerial = (EditText) view.findViewById(R.id.editText_serial);
ImageButton ib = (ImageButton) view.findViewById(R.id.button_scan_serial);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity) getActivity()).scanSerial();
}
});
onActivityResult&我的MainActivity
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
String scanContent = intent.getStringExtra("SCAN_RESULT");
String scanFormat = intent.getStringExtra("SCAN_RESULT_FORMAT");
switch (requestCode){
case (10):{
String[] Split = scanContent.split("\\s");
String product = Split[0];
String label = Split[1];
String serial = Split[2];
mydb.insertData(product, label, serial, mac, daaid, bill);
Fragment fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new ListViewFragment();
ft.replace(R.id.container, fragment);
ft.commitAllowingStateLoss();
break;
}
case (11):{
scannedResult = scanContent;
break;
}
}
} else if (resultCode == RESULT_CANCELED) {
Log.d("TEST", "result_canceled");
}
}
public void scanSerial(){
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A");
startActivityForResult(intent, 11);
}
我知道我应该在扫描序列后收到字符串,但我不知道怎么做。如果onActivityResult
方法设置了片段中的scanningResult字符串,我可以听吗?
答案 0 :(得分:1)
如果您需要扫描条形码而您正在使用zxing应用程序,最好使用zxing的IntentIntegrator。
在您的scanSerial中会有
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan();
然后在MainActivity中的onActivityResult中创建新的Fragment并将结果传递给它
ft.commitAllowingStateLoss();
ft.onActivityResult(requestCode, resultCode, data);
或所有片段(如果您需要
)for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
然后使用Fragment code
中的EditText进行操作IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
String scannedString = scanResult.getContents();
...
}
答案 1 :(得分:1)
只需调用您的扫描方法并定义" your_intent_code。
public void scan() {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A");
startActivityForResult(intent, "your_intent_code);
}
依赖于意图代码,您可以在onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
String scanContent = intent.getStringExtra("SCAN_RESULT");
switch (requestCode){
case (your_intent_code):{
// DO SOMESTHING
}
}
} else if (resultCode == RESULT_CANCELED){
}
}
答案 2 :(得分:0)
我使用此代码扫描插入我的序列号。不完美,但目前它的工作原理:
inputSerial = (EditText) view.findViewById(R.id.editText_serial);
ImageButton ib = (ImageButton) view.findViewById(R.id.button_scan_serial);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity) getActivity()).scanSerial();
}
});
if ((((MainActivity) getActivity()).scannedResult) != null );{
inputSerial.setText(((MainActivity) getActivity()).scannedResult);
}