【问题标题】:How to get different images to display for different EditText inputs?如何为不同的 EditText 输入显示不同的图像?
【发布时间】:2014-02-27 22:20:54
【问题描述】:

我的 XML 代码中有一个 ImageView,并且希望根据 EditText 框中的输入来更改 ImageView 中的图片。我怎样才能做到这一点?下面的 if else 子句给了我一个错误,我的应用程序崩溃了。

else if(compare.equalsIgnoreCase("sweet shoppe")){
            ImageView image = (ImageView) findViewById(R.id.image1);
            image.setVisibility(View.VISIBLE);}

在布局中...

 <ImageView 
     android:id="@+id/image1"
     android:layout_below="@id/answer"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="18dp"
     android:gravity="center" 
     android:src="@drawable/sweet"
     android:visibility="invisible" />

【问题讨论】:

  • 你能把错误日志放在这里吗?
  • 请在您的错误中发布更多代码。
  • 您想要显示不同的图像或修复图像?从你的代码看起来像修复。

标签: android image imageview visibility


【解决方案1】:
   private void displayImg(int value){
    ImageView im = (ImageView)findViewById(R.id.img1);
        if (value >= 0 && value < 10){
        im.setBackgroundResource(R.drawable.animal1);
   }     else if (value >=10 && value <20){
        im.setBackgroundResource(R.drawable.animal2);
   }
           ..... so on...
  }

【讨论】:

    【解决方案2】:

    不要像这样使用 compare.equalsIgnoreCase("sweet shoppe"),这可能会对您有所帮助。

    else if(compare.getText().toString().equalsIgnoreCase("sweet shoppe").trim()){
            ImageView image = (ImageView) findViewById(R.id.image1);
            image.setVisibility(View.VISIBLE);}
    

    【讨论】:

      【解决方案3】:

      尝试使用getText() 方法比较您的EditText 字符串,如下所示。您不能直接比较 EditText 的值。要获取EditText 的值,您需要使用getText() 方法获取它,该方法将返回文本。

      假设您的compare 只是EditText

          else if(compare.getText().toString().equalsIgnoreCase("sweet shoppe")){
               ImageView image = (ImageView) findViewById(R.id.image1);
              image.setVisibility(View.VISIBLE);
            }
      

      【讨论】:

        【解决方案4】:

        从 XML imageView 中删除 srcvisibility 并在 ImageView 运行时设置图像

        这里比较的是从EditText获取的字符串

        试试下面的代码

        else if(compare.equalsIgnoreCase("sweet shoppe")){
        ImageView image = (ImageView) findViewById(R.id.image1);
        img.setImageResource(R.drawable.sweet);
        }

        【讨论】:

          【解决方案5】:

          以下是处理您的问题的代码块


          layout.java 扩展活动文件内容

          public class MainActivity extends Activity {
          
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
          
                  EditText txt = (EditText) findViewById(R.id.editText1);
                  txt.setOnKeyListener(new View.OnKeyListener() {
                      @Override
                      public boolean onKey(View v, int keyCode, KeyEvent event) {
                          if ((event.getAction() == KeyEvent.ACTION_DOWN)
                                  && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                              EditText tx = (EditText) v;
                              String editBoxTxt = tx.getText().toString();
          
                              ImageView img = (ImageView) findViewById(R.id.image1);
                              if (editBoxTxt.equalsIgnoreCase("ring")) {
                                  img.setVisibility(View.VISIBLE);
                                  img.setImageResource(R.drawable.ring);
                              } else
                                  img.setVisibility(View.GONE);
          
                              return true;
                          }
                          return false;
                      }
                  });
          }
          

          layout.xml 文件内容 -->

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@android:color/white"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin" >
          
              <ImageView
                  android:id="@+id/image1"
                  android:layout_width="120dp"
                  android:layout_height="120dp"
                  android:layout_centerHorizontal="true"
                  android:layout_marginTop="18dp"
                  android:gravity="center"
                  android:visibility="gone" />
          
              <EditText
                  android:id="@+id/editText1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/image1"
                  android:layout_centerHorizontal="true"
                  android:layout_marginTop="15dp"
                  android:ems="10"
                  android:inputType="text"
                  android:maxLines="1" >
          
                  <requestFocus />
              </EditText>
          </RelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 2019-08-08
            • 2021-01-03
            • 1970-01-01
            • 2014-12-10
            • 1970-01-01
            • 2019-03-22
            • 2019-04-21
            • 2020-04-25
            • 1970-01-01
            相关资源
            最近更新 更多