如何在不是MainActivity的android的Java类中添加zxing库

时间:2018-08-22 11:19:23

标签: java android xml android-studio zxing

对于我的小项目,我必须创建一个包含3个android页面的应用程序,其中一个按钮即可读取Qr代码。

我发布的第一个代码称为“待添加”,它是插入到具有单个页面的MainActivity中的一段代码,可以正常工作,但是如果我在扫描仪页面上粘贴粘贴,则该代码将无法正常工作。作为初学者,我不知道如何正确实现要添加到第三页“扫描仪”的代码。

所以我的问题是: 如何在scanner.java文件中实现“添加到”代码?

非常感谢:)

这是MainActivity.java

import android.os.Bundle;
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.view.MenuItem;


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.action_item1:
                                selectedFragment = scadenze.newInstance();
                                break;
                            case R.id.action_item2:
                                selectedFragment = prodotti.newInstance();
                                break;
                            case R.id.action_item3:
                                selectedFragment = scanner.newInstance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

“ Scanner.java”

这是第一页(使用botton扫描),我应在其中实施“待添加”代码

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class scanner extends Fragment{
    public static scanner newInstance() {
        scanner fragment = new scanner();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.scanner, container, false);
    }
}

“要添加”

这是我无法正确实现的代码

scan_button = (Button) findViewById(R.id.scan_button);
        final Activity activity = this;       //controllare scanner con Activity
        scan_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                integrator.setPrompt("Scan");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
                integrator.setBarcodeImageEnabled(false);
                integrator.initiateScan();
            }
        });
    }
        @Override
        protected void onActivityResult(int requestCode,int resultCode, Intent data){
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if(result != null){
                if(result.getContents() == null) {
                    Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
                }
            }
            else{
            super.onActivityResult(requestCode, resultCode, data);
        }

另一个代码

“活动main.xml”

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="it.uni.MainActivity">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:animateLayoutChanges="true">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/elenco_stati_oggetto_nav"
        app:itemTextColor="@color/elenco_stati_oggetto_nav"
        app:menu="@menu/bottone_navigazione"/>
</RelativeLayout>

“ scanner.xml”

<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"
    tools:context="it.uni.scanner">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Scannerizza il Codice"
        android:textSize="35sp"/>

    <ImageView
        android:id="@+id/truiton_image"
        android:layout_width="342dp"
        android:layout_height="268dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:src="@mipmap/scan_qrcode" />

    <Button
        android:id="@+id/scan_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="119dp"
        android:text="Scanner" />


</RelativeLayout>

0 个答案:

没有答案