在Android中的不同活动上生成QR码

时间:2018-07-27 09:57:19

标签: android

我一直在尝试创建QR码生成器,该生成器在单击按钮时在单独的活动中生成QR码。下面是我的代码,但它会压住按钮单击。我哪里出问题了?

第一类是MainActivity.class:

package com.example.profmox.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.journeyapps.barcodescanner.BarcodeEncoder;

public class MainActivity extends AppCompatActivity {
    TextView tx1;
    Button btn1, btn2;
    EditText edt1,edt2;
    CheckBox ch;
    String text2qr;

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

        edt1 = (EditText)findViewById(R.id.edit1);
        edt2 = (EditText)findViewById(R.id.edit2);
        btn1 = (Button)findViewById(R.id.login);
        btn2 = (Button)findViewById(R.id.signup);
        ch = (CheckBox)findViewById(R.id.check);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("EditText", edt1.getText().toString());
                Log.v("EditText", edt2.getText().toString());

          //      text2qr = edt1.getText().toString().trim();
          //      MultiFormatWriter mfw = new MultiFormatWriter();
          //      try{
          //          BitMatrix btm = mfw.encode(text2qr, BarcodeFormat.QR_CODE,250,250);
          //          BarcodeEncoder ben = new BarcodeEncoder();
          //          Bitmap bmap = ben.createBitmap(btm);
           //         UserScreen.qr.setImageBitmap(bmap);
          //      }catch (WriterException e){
          //          e.printStackTrace();
          //      }

                startActivity(new Intent(MainActivity.this, UserScreen.class));
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SignUp.class));
            }
        });
    }
}

MainActivity类的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingLeft="18dp"
    android:paddingRight="18dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/art_background"
    tools:context="com.example.profmox.myapplication.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:layout_width="300dp"
                android:layout_height="230dp"
                android:layout_gravity="center_horizontal"
                android:padding="16dp"
                android:layout_marginTop="20dp"
                android:src="@mipmap/logo"/>
            <EditText
                android:id="@+id/edit1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textEmailAddress"
                android:ems="10"
                android:background="@android:color/transparent"
                android:drawablePadding="12dp"
                android:padding="8dp"
                android:hint="Username"
                android:textColorHint="#fff"
                android:maxLines="1"
                android:drawableLeft="@drawable/girl"
                android:layout_marginTop="70dp"
                />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#F2CC8F"/>

            <EditText
                android:id="@+id/edit2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPassword"
                android:ems="10"
                android:background="@android:color/transparent"
                android:drawablePadding="12dp"
                android:padding="8dp"
                android:hint="Password"
                android:textColorHint="#fff"
                android:maxLines="1"
                android:layout_marginTop="4dp"
                />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#F2CC8F"/>

            <CheckBox
                android:id="@+id/check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:textColor="#fff"
                android:text="Remember Me"
                android:padding="9dp"
                />
            <Button
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/login_art"
                android:text="Login"
                android:textColor="#3D405B"
                android:textAllCaps="false"
                android:padding="16dp"
                android:clickable="true"
                style="@style/Base.TextAppearance.AppCompat.Body1"
                android:layout_marginTop="23dp"
                android:textSize="18sp"/>
            <Button
                android:id="@+id/signup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/signuo_art"
                android:text="Sign up"
                android:textColor="#fff"
                style="@style/Base.TextAppearance.AppCompat.Body1"
                android:textAllCaps="false"
                android:textSize="18sp"
                android:layout_marginTop="16dp"
                android:clickable="true"
                android:padding="16dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Forgot your password?"
                android:clickable="true"
                style="@style/Base.TextAppearance.AppCompat.Body2"
                android:padding="16dp"
                android:layout_marginBottom="12dp"/>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

我要在其中生成QR代码的UserScreen.class:

package com.example.profmox.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class UserScreen extends AppCompatActivity {
    static ImageView qr;

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

        qr = (ImageView)findViewById(R.id.qrCode);
    }
}

UserScreen.class的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/art_background"
    android:paddingLeft="18dp"
    android:paddingRight="18dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.profmox.myapplication.UserScreen">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/qrCode"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:layout_gravity="center_horizontal"/>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

0 个答案:

没有答案
相关问题