繁体   English   中英

突出显示在Android Studio中输入文本

[英]Highlight entering text in android studio

我想比较编辑文本和查看文本,并以绿色突出显示正确的单词和以红色突出显示错误的单词。 但是对于任何正确的单词,此代码会将所有错误的单词涂成绿色,将整个文本涂成红色。 请帮忙..

    tview = (TextView) findViewById(R.id.tview); 
    tview1 = (TextView) findViewById(R.id.tview1); 

    simpleEditText = (EditText) findViewById(R.id.simpleEditText); 
    fullStory=tvpass1.getText().toString(); 
    etValue=simpleEditText.getText().toString(); 


    new CountDownTimer(10000, 1000) {

        public void onTick(long millisUntilFinished) {
            tview.setText("Time:  " + millisUntilFinished / 1000);
            tview.setTextColor(Color.BLUE);
        }

        public void onFinish() {
            tview.setText("Time Up!");
            tview.setTextColor(Color.RED);
            simpleEditText.setEnabled(false);
            simpleEditText.clearFocus();

        }
    }.start();

    simpleEditText. addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s,  int start, int count, int after) {
            }
            @Override

            public void onTextChanged(CharSequence s, int start, int count, int before) {

                String etValue= simpleEditText. getText().toString();

//text=text.replace("\\n“,”“);

                String[] EtStArray = etValue.split("\\s+");
                String[] VtStArray = fullStory.split("\\s+");  
//              tview.setText("Words =" +EtStArray.length);

            if((EtStArray.length>1) && ( j!=EtStArray.length))
                {
                    //      word=EtStArray[i];


                    if(EtStArray[i].equals(VtStArray[i]))
                    {     
                        tview1.append(EtStArray[i]+" ");
                        tview1.setTextColor(Color.GREEN);
                    }
                    else
                    {     
                        tview1.append(EtStArray[i]+" ");
                        tview1.setTextColor(Color.RED);
                    }
                    i++;
                    j=EtStArray.length;
                }  

         //   tview1.setText(etValue);
            //tview1.setText(VtStArray[0]);
        }  

     @Override
     public void afterTextChanged( Editable s) {
     } 
     }) ; 

     }
     }

您可以像下面的代码示例一样使用SpannableStringBuilder

  textView = (TextView) findViewById(R.id.textview);
  SpannableStringBuilder builder = new 
  SpannableStringBuilder(textView.getText().toString());

  ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
  ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);
  ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);
  ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);
  ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);


  builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    //setSpan(Object what, int start, int end, int flags)
    //Mark the specified range of text with the specified object.

  textView.setText(builder);

关于更多https://developer.android.com/reference/android/text/SpannableStringBuilder

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM