片段中的onActivityResult不会更新edittext

时间:2014-11-04 15:26:43

标签: java android barcode

我有一个使用this库的条形码扫描器应用程序示例 onActivityResult不会更新textView,也不会更新editText 我做了一些搜索,发现这是一个常见的问题 onActivityResult我得到了我通过的值,我可以记录并烘烤值,但不能在setText()方法上工作。

修改:我正在使用片段  片段A是按钮,编辑和放大器的位置。 textview是,片段B是条形码扫描器,我使用popBackStack()返回片段A(从片段B接收数据);

这是我的代码:

package com.gilbert.apptastic.barcodefragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
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 PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    public static final int DIALOG_FRAGMENT = 1;
    public TextView textView;
    public TextView textView2;
    public TextView textView3;
    public TextView textView4;
    public EditText editText;
    public Button button;
    public String barcode;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        textView = (TextView)rootView.findViewById(R.id.textView);
        textView2 = (TextView)rootView.findViewById(R.id.textView2);
        textView3 = (TextView)rootView.findViewById(R.id.textView3);
        textView4 = (TextView)rootView.findViewById(R.id.textView4);
        editText = (EditText)rootView.findViewById(R.id.editText);
        button = (Button)rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scanBarcode();
            }
        });
//        setText();
        return rootView;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode == DIALOG_FRAGMENT && resultCode == Activity.RESULT_OK) {
//            System.out.println("ok");

            barcode = data.getExtras().getString("barcode");
//            edit_barcode.setText(value);
            setText();
            Toast.makeText(getActivity(), barcode, Toast.LENGTH_SHORT).show();
//            System.out.println(barcode);
//            edit_barcode.setText(barcode);
        }
    }

    public  void setText() {
        System.out.println(barcode);
        textView.setText(barcode);
        textView2.setText(barcode);
        textView3.setText(barcode);
        textView4.setText(barcode);
        editText.setText(barcode);
    }

    public void scanBarcode(){

        Barcode barcode = new Barcode();
        barcode.setTargetFragment(PlaceholderFragment.this, DIALOG_FRAGMENT);
        FragmentTransaction fragmentTransaction =
                getActivity().getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, barcode);
        fragmentTransaction.addToBackStack("VIEW");
        fragmentTransaction.commit();
    }
}  

条形码类:

package com.gilbert.apptastic.barcodefragment;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import me.dm7.barcodescanner.zbar.Result;
import me.dm7.barcodescanner.zbar.ZBarScannerView;

public class Barcode extends Fragment implements ZBarScannerView.ResultHandler {
    private ZBarScannerView mScannerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mScannerView = new ZBarScannerView(getActivity());
        return mScannerView;
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
    }

    @Override
    public void handleResult(Result rawResult) {
        if(!rawResult.getContents().isEmpty()){
        mScannerView.startCamera();
//        Toast.makeText(getActivity(),"Success",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.putExtra("barcode", rawResult.getContents());
        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
        getFragmentManager().popBackStack();

        }else {
//            Toast.makeText(getActivity(),"Fail",Toast.LENGTH_SHORT).show();
            getFragmentManager().popBackStack();
            mScannerView.startCamera();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();
    }
}

我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Scan"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/button">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView2"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textView3"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textView4"
            android:layout_gravity="center_horizontal" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:hint="Barcode"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

试试这些

1 )而不是

barcode = data.getExtras

barcode = data.getStringExtra("barcode")

2 )而不是启动条形码

String barcode;

尝试在onActivityResult中执行此操作,

String barcode = data.getExtras().getString("barcode");

3 )不要将数据直接放入意图中,而是使用捆绑包。

Bundle b = new Bundle();
b.putString ("barcode", rawResult.getContent());
Intent i = new intent();
i.putExtras (b);

在你的onActivityResult

Bundle b = getIntent.getExtras();
String s = b.getString("barcode");

然后在需要的地方使用s。