我的问题很简单
在我的布局中,我有一个 EditText 和两个 ImageView
我在两个 ImageView 中输入任何单词 EditText,我必须根据字母显示图像,将其作为 EditText 调用的单词发送,例如,当您想调用一个字母时,我的问题就出现了:
如果我输入单词“home”应该进入单词并根据字母显示两个 ImageView 图像然后将显示 C(图像 C)然后 A(图片 A)
问题是我可以做一个寻找一个字母的过程,我试过用for但只认出最后一个字母,我也试过做一种延迟(delay)但没成功
我的部分代码:
public class deletreo extends Activity {
protected TextView tv;
protected EditText etxt;
protected ImageView img,img2;
final Handler handle = new Handler();
protected void mth(){
Thread t = new Thread(){
public void run(){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
handle.post(proceso);
}
};
t.start();
}
final Runnable proceso = new Runnable(){
public void run(){
letra();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
tv = new TextView(this);
setContentView(R.layout.deletreo);
etxt = (EditText)findViewById(R.id.text);
Button btn = (Button)findViewById(R.id.btn7);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mth();
}
});
}//fin bundle
private void letra() {
String t = etxt.getText().toString();
char[] array = t.toCharArray();
int p = array.length;
for(int j=0; j<p;j++){
if(array[j] == 'a' || array[j] == 'A'){
img = (ImageView)findViewById(R.id.img);
img.setImageResource(R.drawable.aa);
img2 = (ImageView)findViewById(R.id.img2);
img2.setImageResource(R.drawable.image_1);
onStop();
}
if(array[j] == 'b' || array[j] == 'B'){
img = (ImageView)findViewById(R.id.img);
img.setImageResource(R.drawable.bb);
img2 = (ImageView)findViewById(R.id.img2);
img2.setImageResource(R.drawable.image_2);
}
知道如何解决这个问题吗?
最佳答案
the problem is that I can make the process of finding one letter, I've tried with a for but only recognizes the last letter, I also tried to make a kind of delay (delay) but did not work
当然,它的行为方式是这样的。因为您在启动时延迟了 1 秒,之后您调用了方法 letra()
,该方法在循环中的两个 ImageView 中设置图像资源。因此您只能看到与最后一个字母关联的图像。
试试这样:
public class deletreo extends Activity {
protected TextView tv;
protected EditText etxt;
protected ImageView img,img2;
final Handler handle = new Handler();
protected void mth(){
Thread t = new Thread(){
public void run(){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
};
t.start();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
tv = new TextView(this);
setContentView(R.layout.deletreo);
etxt = (EditText)findViewById(R.id.text);
Button btn = (Button)findViewById(R.id.btn7);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
letra();
}
});
}//fin bundle
private void letra() {
String t = etxt.getText().toString();
char[] array = t.toCharArray();
int p = array.length;
for(int j=0; j<p;j++){
if(array[j] == 'a' || array[j] == 'A'){
img = (ImageView)findViewById(R.id.img);
img.setImageResource(R.drawable.aa);
img2 = (ImageView)findViewById(R.id.img2);
img2.setImageResource(R.drawable.image_1);
onStop();
}
if(array[j] == 'b' || array[j] == 'B'){
img = (ImageView)findViewById(R.id.img);
img.setImageResource(R.drawable.bb);
img2 = (ImageView)findViewById(R.id.img2);
img2.setImageResource(R.drawable.image_2);
}
mth();
}
}
}
每轮循环后调用延迟函数mth()。希望它有效。
关于java - 在 Android 中用图像拼写单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14038207/