如何使用 java 实时更新带有 EditText 的 TextView

How to update TextView with EditText realtime using java

嘿伙计们和姑娘们,我无法找出最能解决我的问题的过程和方法 problem.I 有一个应用程序,其中用户创建的文本以秘密 font/language 显示,而英语(非秘密)文本显示在文本视图中。

JAVA

import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.view.KeyEvent;

public class MainActivity extends ActionBarActivity {
TextView tv;
EditText et;
private Button convert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Loading our AvianKingdom Codex Font
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/WWAvianKingdom-Regular.ttf");
    //Text view label
    tv = (TextView) findViewById(R.id.CustomFontText);
    et = (EditText) findViewById(R.id.CodexMessage);
    //Applying the font
    et.setTypeface(tf);
    //Convert the text
    et.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_ENTER:
                        String inputText = et.getText().toString();
                        tv.setText(inputText);
                        return true;
                    default:
                        break;
                }
            }
            return false;
        }
    });
}       

在我的应用程序中,我试图在用户输入文本时更新文本视图,但到目前为止,我只能根据 click/press 的 button.My 问题进行更改,

How do I update TextView with EditText in realtime using java inside Android Studio?

我想在点击 send/enter 按钮之前,在用户输入阶段执行此操作。您认为最好的解决方案是什么?我查过 TextWatcher/KeyListener/KeyClick 但刚接触 Java 并不能帮助我完全决定或理解。

想通了!!! 我最终进行了大量研究并找到了一个非常有用的博客 post。这是答案

  package com.christianlopez.helloworld;
 //De-Raptor-Codex Android Prototype

import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;
import android.widget.EditText;
import android.text.TextWatcher;
import android.text.Editable;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Log;
import android.graphics.Typeface;
import android.content.Intent;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ByteArrayOutputStream;

public class MainActivity extends ActionBarActivity {
TextView CodexTV;
EditText CodexET;

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

    //Loading our AvianKingdom Codex Font
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/WWAvianKingdom-Regular.ttf");
    //Text view label
    CodexET = ((EditText) findViewById(R.id.CodexMessage));
    //REAL-TIME Textview change
    CodexET.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {   //Convert the Text to String
            String inputText = CodexET.getText().toString();
            CodexTV = ((TextView) findViewById(R.id.CustomFontText));
            CodexTV.setText(inputText);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }
    });

    //Applying the font
    CodexET.setTypeface(tf);

    //Screenshot our Codex'ed Image
    CodexET.setOnKeyListener(new OnKeyListener()
    {
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                        doShare(getDefaultIntent());
                        captureScreen();
                    case KeyEvent.KEYCODE_ENTER:
                        doShare(getDefaultIntent());
                        captureScreen();
                    default:
                        break;
                }
            }
            return false;
        }
    });
}

答案从e.addtextChanged听众开始。这会将应用程序设置为关注 EditText 中的任何更改,然后我将其转换为字符串,并实时将其转发回 Textview。

只是尽量让您的回答简短...这是 .addTextChangedListener 方法,我们在 afterTextChanged 方法中对其进行编码。

CodexET.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {   //Convert the Text to String
        String inputText = CodexET.getText().toString();
        CodexTV = ((TextView) findViewById(R.id.CustomFontText));
        CodexTV.setText(inputText);
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Does not do any thing in this case
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Does not do any thing in this case
    }
});