更改密码正确的图像

发布于 2024-09-26 13:15:02 字数 2583 浏览 3 评论 0原文

所以我是一个完全的新手,目前正在学习移动编程入门课程,其中我们使用 Android(我有一些 Java 经验)。我正在尝试做一个简单的作业,显示一个文本字段和一个图像,输入正确的“密码”并按 Enter 键后,图像会发生变化。

应该就是这么简单!但我对此感到非常困难,即使在进行了大量搜索之后也无法弄清楚我做错了什么(我认为这是非常明显的事情,而我错过了它)。

这是我的代码:

package CS285.Assignment1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ImageView;

public class DisplayImage extends Activity 
      implements OnKeyListener{

 private EditText enteredText;
 private String pass = "monkey";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        enteredText = (EditText)findViewById(R.id.passField);
        enteredText.setOnKeyListener(this);    

    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)){
          // Perform action on key press

         switchImage();

          return true;
        }
        return false;
    }

    public void switchImage(){

  if(enteredText.getText().toString() == pass){
   ImageView imgView = (ImageView)findViewById(R.id.Image);
         imgView.setImageResource(R.drawable.marmoset);
  }     
    }

}

和我的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView android:id="@+id/textPrompt" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:background="#ff993300" 
  android:text="Please enter password to see my real picture:" 
 >
 </TextView>
 <EditText android:id="@+id/passField"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
    >
        </EditText>
<ImageView 
 android:id="@+id/Image"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:adjustViewBounds="true" 
 android:src="@drawable/airplane"
/>


</LinearLayout>

我一开始以为我没有正确从“enteredText”中提取字符串,因此与“password”的比较没有正确发生,但我后来尝试只打印enteredText而且效果很好。

完全沮丧——任何帮助将不胜感激!

丹尼尔

So I am a complete newb, and am currently taking an intro to Mobile Programming course in which we use Android (I have some experience with Java). I am trying to do a simple assignment which displays a text field and an image, and upon entering the correct "password" and pressing enter, the image changes.

Should be so simple! But I am having a really hard time with this and can't figure out what I am doing wrong, even after doing a good bit of searching (I assume it is something super obvious and I'm missing it).

Here is my code:

package CS285.Assignment1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ImageView;

public class DisplayImage extends Activity 
      implements OnKeyListener{

 private EditText enteredText;
 private String pass = "monkey";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        enteredText = (EditText)findViewById(R.id.passField);
        enteredText.setOnKeyListener(this);    

    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)){
          // Perform action on key press

         switchImage();

          return true;
        }
        return false;
    }

    public void switchImage(){

  if(enteredText.getText().toString() == pass){
   ImageView imgView = (ImageView)findViewById(R.id.Image);
         imgView.setImageResource(R.drawable.marmoset);
  }     
    }

}

and my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView android:id="@+id/textPrompt" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:background="#ff993300" 
  android:text="Please enter password to see my real picture:" 
 >
 </TextView>
 <EditText android:id="@+id/passField"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
    >
        </EditText>
<ImageView 
 android:id="@+id/Image"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:adjustViewBounds="true" 
 android:src="@drawable/airplane"
/>


</LinearLayout>

I thought at first that I was not properly extracting the String from "enteredText" so the comparison to the "password" wasn't happening correctly, but I have since tried just printing the enteredText and it works fine.

Totally frustrated--Any help would be greatly appreciated!

Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心作怪 2024-10-03 13:15:02

if(enteredText.getText().toString() == pass) 应该是 if(enteredText.getText().toString().equals(pass))

从风格上来说,您不应该在切换图像函数中进行检查 - 您应该检查密码是否匹配,然后调用切换图像函数。

if(enteredText.getText().toString() == pass) should be if(enteredText.getText().toString().equals(pass)).

As a stylistic matter, you should not do the checking within the switch image function - you should check that the password matches and then call the switch image function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文