AIDE 练字

  • Post author:
  • Post category:其他


在这里插入图片描述

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/bt"
        android:textSize="20dp"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/nr"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
package com.lian.zi;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.EditText;
import java.util.Random;
import android.text.TextWatcher;
import android.text.Editable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.graphics.Color;
import android.text.Spanned;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

    public String[] a,b;
    public TextView bt;
    public EditText nr;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        a = "A、B、C、D、E、F、G、H、I、J、K、L、M、N、O、P、Q、R、S、T、U、V、W、X、Y、Z".split("、");
        b = "a、b、c、d、e、f、g、h、i、j、k、l、m、n、o、p、q、r、s、t、u、v、w、x、y、z".split("、");
        bt = findViewById(R.id.bt);
        nr = findViewById(R.id.nr);
        nr.addTextChangedListener(new TextWatcher(){
                public void afterTextChanged(Editable s)
                {
                }
                public void beforeTextChanged(CharSequence s, int start, int count, int after)
                {
                }
                public void onTextChanged(CharSequence s, int start, int before, int count)
                {

                    int cd=s.length();
                    CharSequence t=bt.getText();
                    if (cd <= t.length())
                    {
                        SpannableString msp = new SpannableString(t);
                        int i=0;
                        for (i = 0;i < cd;i++)
                        {
                            char s1=s.charAt(i);
                            char s2=t.charAt(i);
                            if (s1 == s2)
                            {
                                msp.setSpan(new BackgroundColorSpan(-12871123), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置背景色为青色
                                bt.setText(msp);  
                            }
                            else
                            {
                                msp.setSpan(new BackgroundColorSpan(Color.RED), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置背景色为青色
                                bt.setText(msp);
                            }
                        }
                        msp.setSpan(new BackgroundColorSpan(0), i, t.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置背景色为青色
                        bt.setText(msp);
                    }
                }
            });
        Thread thread2= new Thread(new Runnable() {
                @Override
                public void run()
                {
                    while (true)
                    {
                        String c=bt.getText().toString().toLowerCase();
                        String d=nr.getText().toString().toLowerCase();
                        if (c.equals(d))
                        {
                            showResponse();
                        }
                        try
                        {
                            Thread.sleep(500);
                        }
                        catch (InterruptedException e)
                        {}
                    }
                }
            });
        thread2.start();//启动线程
    }
    //修改UI
    private void showResponse()
    {
        runOnUiThread(new Runnable() {
                @Override
                public void run()
                {
                    String str="";
                    for (int i=0;i < 15;i++)
                    {
                        str += a[sj()] + " ";
                    }
                    str += a[sj()];
                    bt.setText(str);
                    nr.setText("");
                }    
            }); 
    }
    public int sj()
    {
        int min = 0;
        int max = 24;
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }
}



版权声明:本文为qq_25226575原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。