我正在尝试编码zxing可以读取条形码并将值返回到其中一个文本字段
我正在关注此文件:Fragment example
应用程序将启动并将被复制到剪贴板,但数字不会出现在TextFiled中。
这是我的XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/TabFirst">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="First Tab"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:id="@+id/buttonTest"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/boxCodeInput"
android:layout_below="@+id/buttonTest"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/buttonTest"
android:layout_alignStart="@+id/buttonTest" />
和这是ma java文件:
package com.example.user.myapplication;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class first extends Fragment {
private EditText text1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first, container, false);
text1 = (EditText) rootView.findViewById(R.id.boxCodeInput);
Button firstButton ;
firstButton = (Button) rootView.findViewById(R.id.buttonTest);
firstButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Button Pressed!", Toast.LENGTH_SHORT).show();
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String scanData = (scanningResult != null) ? scanningResult.getContents() : "";
if (scanData == null || scanData.isEmpty()) {
Toast.makeText(getActivity(), "TryAgain!!", Toast.LENGTH_SHORT).show();
} else {
text1.setText(scanData);
}
}
}
答案 0 :(得分:0)
将 onActivityResult 移动到main之后,它就起作用了 谢谢大家!