这是我扫描条形码并将内容设置为 TextView 的代码:
np.linspace
在public class scanCodeFragment extends android.support.v4.app.Fragment implements View.OnClickListener{
Button btnScan;
TextView contentTxt;
String substrCode, scanContent;
View v;
public scanCodeFragment(){
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_scan_code, container, false);
btnScan = (Button) v.findViewById(R.id.btnScan);
contentTxt = (TextView) v.findViewById(R.id.scan_content);
btnScan.setOnClickListener(this);
return v;
}
public void onClick(View v){
if (v.getId() == R.id.btnScan){
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
scanIntegrator.initiateScan();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
scanContent = scanningResult.getContents();
substrCode = scanContent.substring(2, 10);
contentTxt.setText("NIS: " + substrCode);
}
else {
Toast toast = Toast.makeText(getActivity(), "Kode barcode salah", Toast.LENGTH_SHORT);
toast.show();
}
}
}
方法中,我无法在扫描条形码代码后得到结果。在此代码片段之前,我可以从onActivityResult获取结果。
答案 0 :(得分:0)
我不知道你的initiateScan()如果你需要它会保留它
final int requestCode = 100;
public void onClick(View v){
if (v.getId() == R.id.btnScan){
IntentIntegrator intent = new IntentIntegrator(getContext(), MainActivity.class);
intent.putExtra(RECIPE_EXTRA, extraObject);
startActivityForResult(intent, requestCode);
}
}
答案 1 :(得分:0)
从您提供的代码段中,我建议您创建一个单独的Activity
用于扫描,而不是Class
文件,在您的情况下为IntentIntegrator
,然后启动它当您点击扫描Button
时。
所以,创建IntentIntegratorActivity
并像这样开始。
public void onClick(View v){
if (v.getId() == R.id.btnScan){
IntentIntegratorActivity intent = new Intent(getActivity(), IntentIntegratorActivity.class);
startActivityForResult(intent, SCAN_INTENT_CODE);//Remember this code, which is an Integer value
}
}
然后,在OnCreate
的{{1}}方法中,进行扫描并通过IntentIntegratorActivity
返回扫描数据。
Intent
然后,应在Intent returnIntent = new Intent();
returnIntent.putExtra("SCAN_DATA_KEY", scanData);//Send scan data from here and remember the key SCAN_DATA_KEY
setResult(RESULT_OK, returnIntent);
finish();
onActivityResult
中的Fragment
方法内捕获此数据。
Class
答案 2 :(得分:0)
尝试将此行IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
更改为此IntentIntegrator scanIntegrator = new IntentIntegrator(this);
。
原因是如果你在IntentIntegrator中传递一个Activity作为参数,那么在initiateScan
上将调用activity.startActivityForResult(intent, code)
- 因此结果将不会传递给片段(尝试检查父活动的onActivityResult,它将被称为)。但是如果你将Fragment作为IntentIntegrator中的参数传递 - 将被称为fragment.startActivityForResult(intent, code)
,那么Android框架将能够在你的片段中调用onActivityResult以获得结果。
答案 3 :(得分:0)
OnActivityResult()仅在您调用时调用 startActivityForResult()
我在Fragment中调用startActivityForResult()并在Fragment中实现onActivityResult,将正确调用onActivityResult()。
你不能在activity中调用startActivityForResult(),否则不会调用Fragment中的onActivityResult()