我有一个应用程序,在这个应用程序中,用户创建的文本会以一种保密的字体语言显示,而英文(非保密)文本会在文本视图中显示。
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;
}
});
}
在我的应用程序中,我正试图在用户输入文本时更新文本视图,但到目前为止,我只能在点击按钮时进行更改。
How do I update TextView with EditText in realtime using java inside Android Studio?
我想在点击sendenter按钮之前,在用户输入阶段进行。你认为最好的解决方案是什么?我查了一下TextWatcherKeyListenerKeyClick,但是作为一个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
}
});