如何使用java使用EditText实时更新TextView
大家好,我很难找到解决问题的最佳方法。我有一个应用程序,其中用户创建的文本以保密字体/语言显示,而英文(非保密)文本显示在文本视图中 爪哇 在我的应用程序中,我试图在用户输入文本时更新文本视图,但到目前为止,我只能在单击/按下按钮时进行更改。我的问题是如何使用java使用EditText实时更新TextView,java,android,textview,android-edittext,Java,Android,Textview,Android Edittext,大家好,我很难找到解决问题的最佳方法。我有一个应用程序,其中用户创建的文本以保密字体/语言显示,而英文(非保密)文本显示在文本视图中 爪哇 在我的应用程序中,我试图在用户输入文本时更新文本视图,但到目前为止,我只能在单击/按下按钮时进行更改。我的问题是 如何在Android Studio中使用java实时更新带有EditText的TextView? 在用户输入阶段,我想在点击发送/输入按钮之前执行此操作。你认为最好的解决方案是什么?我查过TextWatcher/KeyListener/KeyCl
如何在Android Studio中使用java实时更新带有EditText的TextView?
在用户输入阶段,我想在点击发送/输入按钮之前执行此操作。你认为最好的解决方案是什么?我查过TextWatcher/KeyListener/KeyClick,但对Java不熟悉并不能帮助我做出决定或完全理解它。找到了答案!!!
最后我做了很多研究,发现了一篇非常有用的博客文章。这是答案
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 Listener开始。这将应用程序设置为关注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
}
});
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
}
});