Android - 条码片段结果不显示 [英] Android - Barcode Fragment result not displaying

查看:33
本文介绍了Android - 条码片段结果不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[已解决]应用程序可以正常工作而不会崩溃,但应该将 resultView 文本从Hasil Scan"更新为扫描结果,但事实并非如此.问题是文本视图(resultView)在扫描后没有更新.我使用 DM77 Zxing 条码扫描器.这是我到目前为止所做的代码.

[SOLVED] The apps work fine without crash, but it should be update the resultView text from "Hasil Scan" to the scan result, but it doesnt. The problem are the textview (resultView) are not updated after scan. Im using DM77 Zxing barcode scanner. Here is the code i have done so far.

主要活动:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import sc.smklabor.rpl.labsscanner.sc.smklabor.rpl.labsscanner.fragments.ScanFragment;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.scannav:
                                selectedFragment = ScanFragment.instance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, ScanFragment.instance());
        transaction.commit();

    }
}

这里是扫描仪片段,我只做了一个片段,其他片段只是关于和网站扫描片段:

and here is for the scanner fragment, i only have done one fragment, other fragment just will be about and website ScanFragment:

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import sc.smklabor.rpl.labsscanner.R;

/**
 * Created by Lenovo on 21/03/2018.
 */

public class ScanFragment extends Fragment implements View.OnClickListener {

    private IntentIntegrator scanner;
    private Button startScan,btnGoUrl;
    private TextView resultView;
    private String scanResult;

    public static ScanFragment instance() {
        ScanFragment f = new ScanFragment();
        return f;
    }

    public void onCreate(Bundle b) {
        super.onCreate(b);
        scanner = new IntentIntegrator(this.getActivity()).forSupportFragment(this);
    }

    public void onActivityResult(int rc,int res, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(rc, res, data);
        if(result!=null) {
            scanResult = result.getContents().toString();
            if (scanResult.contains("--L--:")) {
                String postId = scanResult.split("://")[1];
                btnGoUrl.setVisibility(View.INVISIBLE);
                resultView.setText("Post ID: " + postId);
            } else if (URLUtil.isHttpUrl(scanResult) || isValidURL(scanResult)) {
                btnGoUrl.setVisibility(View.VISIBLE);
                resultView.setText(result.getContents().toString());
            } else {
                btnGoUrl.setVisibility(View.INVISIBLE);
                resultView.setText(result.getContents().toString());
            }
        }
    }

    public View onCreateView(LayoutInflater inf, ViewGroup con, Bundle b) {
        View view = inf.inflate(R.layout.scanlayout, con, false);
        startScan = (Button)view.findViewById(R.id.button);
        btnGoUrl = (Button)view.findViewById(R.id.btnGoUrl);
        scanner = new IntentIntegrator(getActivity());
        startScan.setOnClickListener(this);
        resultView = (TextView)view.findViewById(R.id.textView);

        btnGoUrl.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View e) {
                Uri URL = Uri.parse(scanResult);
                Intent i = new Intent(Intent.ACTION_VIEW,URL);
                startActivity(i);
            }
        });
        return view;
    }
    public boolean isValidURL(String s) {
        return (s.contains("http://") || s.contains("https://")) && (s.contains(".com") || s.contains(".org") || s.contains(".net") || s.contains(".id") || s.contains(".sch.id") || s.contains(".me"));
    }

    public void onClick(View e) {
        scanner.setPrompt("Arahkan kamera kepada barcode tepat pada kotak.");
        scanner.setOrientationLocked(false);
        scanner.initiateScan();
    }
}

推荐答案

我认为问题是 onActivityResult 没有在 ScanFragment

I think the problem is the onActivityResult is not called in the ScanFragment

首先你应该覆盖onActivityResult(int rc,int res, Intent data)MainActivity

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_layout);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

此代码将帮助您调用 ScanFragment 上的 onActivityResult.

This code it will help you call the onActivityResult on the ScanFragment.

这篇关于Android - 条码片段结果不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆