根据输入的文本将图像放入图像视图中

时间:2014-01-02 04:47:38

标签: android

我在运行时有图像视图,在drawable中有字母图像。我希望当用户在编辑文本中输入一些文本时,他在编辑文本中输入的任何内容都会检查字母表并仅将那些alpbhabet图像放在视图中....

如果可能的话请提前帮助 下面是我在运行时放置imageview的代码.....

button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String str=text.getText().toString();
              int num= str.length(); 
              //generating image view at runtime
              ImageView[] imageViewArray = new ImageView[num];
              for(int i=0;i<num;i++)
              {   count=count+1;
                  imageViewArray[i] = new ImageView(getApplicationContext());
            imageViewArray[i].setImageResource(R.drawable.back_view);
            RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                60,60);
                lp.leftMargin=left;
                lp.topMargin=top;
                    rl.addView(imageViewArray[i],lp);
                    imageViewArray[i].setId(count); 
                      left=left+70;
              }
              }
        });

8 个答案:

答案 0 :(得分:1)

按照我的方式。您可以像下面那样绘制并设置为ImageView:

 Drawable drawable = getResources().getDrawable(getResources()
              .getIdentifier(edit_text.getText(), "drawable", getPackageName()));
 imageView.setImageResource(drawable);

您可以将其添加到for循环中并实现您想要的效果。如果我没想错,那么这个解决方案就可以了。

答案 1 :(得分:1)

你有array个字母的图像(确保它们的顺序为A-Z)。你已遍历字符串中的每个字符。只需获取数组中alphabet的位置,如下所示:

int alphabetPosition = str.charAt(i) - (Character.isUpperCase(str.charAt(i)) ? 'A' : 'a');

然后按如下方式设置图像:

imageViewArray[i].setImageResource(photos[alphabetPosition]);

所以最终代码如下所示:

...
...
for(int i=0;i<num;i++)
{   count=count+1;
    imageViewArray[i] = new ImageView(getApplicationContext());
    int alphabetPosition = str.charAt(i) - (Character.isUpperCase(str.charAt(i)) ? 'A' : 'a');
    imageViewArray[i].setImageResource(photos[alphabetPosition]);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative);
...
...

答案 2 :(得分:0)

嗯,你可以这样做。

  1. TextView.OnEditorActionListener设置为edittext,因此一旦用户在edittext中进行更改,您将获得回调方法。
  2. 查找用户已执行的操作(输入的字符,已删除的字符,按下其他输入或删除字符的其他按钮)。
  3. 有一个swich case语句来设置图像视图。或者我更喜欢的最佳选择是获取用户输入的字符然后使用以下代码使用以下代码获取正确的drawable。
  4.   

    getResources()则getIdentifier( “一”, “可拉伸”, “包名”);

答案 3 :(得分:0)

public class MainActivity extends Activity {
EditText ed1;
Button b1;
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    img=(ImageView)findViewById(R.id.imageView1);
    ed1=(EditText)findViewById(R.id.editText1);
    b1=(Button)findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String str=ed1.getText().toString();
            if(str.equals("A"))
            {

                img.setImageResource(R.drawable.a);
            }
            else if(str.equals("B"))
            {
                img.setImageResource(R.drawable.b);
            }

        }
    });

}

这是您需要的代码..简单代码..相应地将其更改为您的修改...如果您喜欢它+1 ..

答案 4 :(得分:0)

您可以使用Text Watcher,   //获取EditText的参考资料

                        // Attach TextWatcher to EditText
                        editText.addTextChangedListener(mTextEditorWatcher);

// EditTextWacther实施

            private final TextWatcher  mTextEditorWatcher = new TextWatcher() {

                public void beforeTextChanged(CharSequence s, int start, int count, int after)
                {
                            // When No Password Entered
                           textViewPasswordStrengthIndiactor.setText("Not Entered");
                }

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

                }

                public void afterTextChanged(Editable s)
                {
                             if(s.equals("A"))
                                   //set Image 

                             else  if(s.equals("B"))
                                   //set Image 

                             //so on

                }
        };

答案 5 :(得分:0)

String[] str = {APPLE,BANANA};
Drawable[] d = {"id of apple image", "id of banana image"};


private final TextWatcher  mTextEditorWatcher = new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                        // When No Password Entered
                       textViewPasswordStrengthIndiactor.setText("Not Entered");
            }

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

            }

            public void afterTextChanged(Editable s)
            {
                        int position = -1;
                        for(i=0;i<str.length;i++){
                           if(s==str[i])
                             position = i;

                            }

                           if(position!=-1)
                                img.setimg(d[position]);

            }
    };

答案 6 :(得分:0)

public class MainActivity extends Activity {
ArrayList<String> strArray = new ArrayList<String>();
int[] drawableArray ={R.drawable.red_apple,R.drawable.banana};
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    strArray.add("APPLE");


    //dArray.add(R.drawable.red_apple);
    //dArray.add(R.drawable.red_apple);
//  dArray.add(R.drawable.ic_launcher);
    EditText et = (EditText)findViewById(R.id.et);
    et.addTextChangedListener(mTextEditorWatcher);

     img = (ImageView)findViewById(R.id.img);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

  private final TextWatcher  mTextEditorWatcher = new TextWatcher() {

      public void beforeTextChanged(CharSequence s, int start, int count, int after)
      {
                  // When No Password Entered
      }

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

      }

      public void afterTextChanged(Editable s)
      {
          int position =-1;
          for (int i = 0; i < strArray.size(); i++) {
              Log.e(""+s.toString(), ""+strArray.get(i).toString());
              if(s.toString().equals(strArray.get(i).toString())){
               position = i;
              }
              Log.e("", ""+position);
              if (position!=-1) {
                  Drawable d = getResources().getDrawable(drawableArray[position]);
                  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawableArray[position]); 
   //                     img.setImageDrawable(d);
                  img.setImageBitmap(bitmap);
            }
        }



      }
  };

}

和xml是

<EditText android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="APPL" />

<ImageView android:id="@+id/img"
    android:layout_below="@id/et"
    android:layout_width="60dip"
    android:layout_height="60dip"
    android:background="@drawable/red_apple" />

答案 7 :(得分:0)

package com.example.textwatcherwithimage;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
ArrayList<String> strArray = new ArrayList<String>();
int[] drawableArray ={R.drawable.ic_launcher};
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    strArray.add("APPLE");


    //dArray.add(R.drawable.red_apple);
    //dArray.add(R.drawable.red_apple);
//  dArray.add(R.drawable.ic_launcher);
    EditText et = (EditText)findViewById(R.id.et);
    et.addTextChangedListener(mTextEditorWatcher);

     img = (ImageView)findViewById(R.id.img);
//       img.setBackgroundResource(R.drawable.red_apple);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

  private final TextWatcher  mTextEditorWatcher = new TextWatcher() {

      public void beforeTextChanged(CharSequence s, int start, int count, int after)
      {
                  // When No Password Entered
      }

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

      }

      public void afterTextChanged(Editable s)
      {
          int position =-1;
          for (int i = 0; i < strArray.size(); i++) {
              Log.e(""+s.toString(), ""+strArray.get(i).toString());
              if(s.toString().equals(strArray.get(i).toString())){
               position = i;
              }
              Log.e("", ""+position);
              if (position!=-1) {
                  Drawable d =     getResources().getDrawable(drawableArray[position]);
                  Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
//                    img.setImageDrawable(d);
                  img.setImageBitmap(bitmap);
            }
        }

                         //set Image 


                         //set Image 

                   //so on

      }
  };

}
找到这个, 在这里,如果我有苹果字符串然后我显示ic_launcher或没有。 你可以尝试根据你的要求修改数组