Android 学习笔记27

继第22篇已经可以绑定fragment跟viewpager那我们就在fragment中加入一些东西吧,今天来试着生成QRcode
首先是xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

    <Button
        android:id="@+id/qrbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="56dp"
        android:text="   生成QRcode   "
        android:background="@drawable/nsq"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/camerabutton"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/camerabutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="88dp"
        android:text="开启相机"
        android:background="@drawable/nsq"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.62"
        app:layout_constraintStart_toEndOf="@+id/qrbutton" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="160dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="请输入网址或文字"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/camerabutton"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="@+id/qrbutton" />

    <ImageView
        android:id="@+id/qrcode"
        android:layout_width="275dp"
        android:layout_height="275dp"
        app:layout_constraintBottom_toTopOf="@+id/edittext"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.722"/>
</androidx.constraintlayout.widget.ConstraintLayout>

再来是

public class ScanActivity extends BaseActivity implements View.OnClickListener{
    Button qrbutton;
    Button camerabutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        qrbutton=findViewById(R.id.qrbutton);
        qrbutton.setOnClickListener(this);
        camerabutton=findViewById(R.id.camerabutton);
        camerabutton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.qrbutton:
                getCode();
                break;
            case R.id.camerabutton:
                start(CameraActivity.class);
                break;
        }
    }
    public void getCode(){
        ImageView ivCode=findViewById(R.id.qrcode);
        EditText etContent=findViewById(R.id.edittext);
        BarcodeEncoder encoder = new BarcodeEncoder();
        try{
            Bitmap bit = encoder.encodeBitmap(etContent.getText().toString()
                    ,BarcodeFormat.QR_CODE,250,250);
            ivCode.setImageBitmap(bit);
        }catch (WriterException e){
            e.printStackTrace();
        }
    }
}

成果如下
https://ithelp.ithome.com.tw/upload/images/20211006/20141791zsW8YtKk67.png


<<:  【Day 21】薛丁格的 Process (下) - Process Hollowing

>>:  Day 21 - 实战演练 — Button / ButtonGroup / IconButton

why
杂谈    

Day47. 组合模式

本文同步更新於blog Composite Pattern 允许将对象组合成树形结构来表现整体/部...

GCP loadbalanc(二)

GCP loadbalance (HTTP(S)) HTTP(S)负载平衡是一种全球性的基於代理的第...

Day 22 : PyAutoGUI,自动控制滑鼠跟键盘!

今天来讲讲PyAutoGUI这个套件好了,这个套件可以帮助你写脚本,可以让滑鼠以及键盘自动化,就不用...

Day26 深入解析Elasticsearch Query DSL Fuzzy query

Hello大家~ 昨天有去看烟火吗? 个人很怕烟火声都是看别人拍好的然後静音观看XD 在之前的内容我...

从 IT 技术面细说 Search Console 的 27 组数字 KPI (3) 点击 (2) 网页搜寻

上一篇提到可以从 Search Console 看到 6 种不同的流量来源,而 SC 提供的概要是用...