当用户在Android的EditText中输入文本时,如何更改许多图像的背景
我有一个包含10个图像的程序。我想在用户在editText中输入有效文本时更改每个图像的背景。所以基本上如果用户在editText中输入有效的文本,它会改变第一张图片(图片1)。如果用户在editText中再次输入文本,它应该改变图像2等,直到图像10.当用户在Android的EditText中输入文本时,如何更改许多图像的背景
我试图创建图像列表并检索图像中的每个元素。
我不知道如果我的逻辑是错误的
的图像是stamp1,STAMP2,stamp3,stamp4 .... stamp12
final String Entercode = codeNumber.getEditableText().toString().trim();
Toast.makeText(getApplicationContext(),Entercode,Toast.LENGTH_SHORT).show();
if (Entercode.equals("sweet")){
for (int i = 0; i < stampImageList.size(); i++) {
Object obj = stampImageList.get(i);
stampImageList = new ArrayList();
stampImageList.add(stamp1);
stampImageList.add(stamp2);
stampImageList.add(stamp3);
stampImageList.add(stamp4);
stampImageList.add(stamp5);
stampImageList.add(stamp6);
stampImageList.add(stamp7);
stampImageList.add(stamp8);
stampImageList.add(stamp9);
stampImageList.add(stamp10);
stampImageList.add(stamp11);
stampImageList.add(stamp12);
if (obj == stampImageList.get(2)) {
// stamp4.setBackgroundResource(R.drawable.earned_stamp);
stamp3.setBackgroundResource(R.drawable.earned_stamp);
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setIcon(R.drawable.logo);
builder.setMessage("Stamp Earned");
} else if (obj == stampImageList.get(3)) {
stamp5.setBackgroundResource(R.drawable.earned_stamp);
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setIcon(R.drawable.logo);
builder.setMessage("Stamp Earned");
}
}
} else{
AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
alert.setIcon(R.drawable.logo);
alert.setTitle("Validation results");
alert.setMessage("validation failed");
}
您应该使用TextWatcher到EditText.In改动后的方法你与价值进行比较。
EditText et = (EditText)findViewById(R.id.editText);
Log.e("TextWatcherTest", "Set text xyz");
et.setText("xyz");
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) {
Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());//Compare here with stamp1 or like that
}
});
@steve,这里我准备了10个可绘制图像的代码在您的项目中。
public class Pictures_Activity_stack extends AppCompatActivity {
private String TAG= "Pictures_Activity---";
private ImageView picture;
private EditText text;
private Button validate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pictures_stack);
picture = (ImageView)findViewById(R.id.picture); //imageview where your picture changes
text = (EditText) findViewById(R.id.text);//edittext where you input text
validate = (Button) findViewById(R.id.button);//button to validate the text and change picture accordingly
// array to store your drawable images
final int pictures[] = {
R.drawable.firstimage,
R.drawable.secondimage,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5,
R.drawable.p6,
R.drawable.p7,
R.drawable.p8,
R.drawable.p9,
R.drawable.p10
};
// click the button to set the image
validate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input = text.getText().toString(); //input from edittext
if (input.equals("first")) {
picture.setImageResource(pictures[0]); //set first image in array if input=first
Toast.makeText(getBaseContext(),input,Toast.LENGTH_SHORT).show();
}
else if (input.equals("second")) {
picture.setImageResource(pictures[1]);//set first image in array if input=secind
Toast.makeText(getBaseContext(),input,Toast.LENGTH_SHORT).show();
}
// else if (input.equals("third")) {
// // and so on for other string values...
// .................................
// }
else
{
// if your input does not matches any string do this
Toast.makeText(getBaseContext(),"NO MATCHED STRING",Toast.LENGTH_SHORT).show();
}
}
});
}
}
上面的代码在点击按钮时根据编辑文本中的输入设置图像。
感谢您的努力。先生。代码很好,但不是很有用。我之前没有提到的是:10张图片的可绘制资源相同。所以可绘制的所有图像值都是R.drawable.image,它们只是具有不同的id。所以我希望通过调用图像的ID我可以改变它的背景,当我在EditText中输入有效的数据。这就是为什么我使用列表来拍摄图像作为对象添加图像并获取列表中的图像索引,然后更改图像的背景。我不知道我是否足够清楚。 – steve
我真的想帮助你,但我仍然不清楚这个问题。您可以分享您活动的_screenshot_。当用户用户输入_code_并显示每个_code_的图像(_stamp_)时,我可以看到您正尝试创建一个_alert_ _dialog_。 – Androwed
使用textwatcher –
你跟当前的代码获得现在是什么结果呢? – Gavin
通过使用我当前的代码它崩溃。应用程序。我的问题是editText上的有效值不应该相同。当我在编辑文本中输入代码时,我希望能够更改一个图像。请帮助 – steve