Android 为什么可以';我不能用编辑文本来写文本吗?

Android 为什么可以';我不能用编辑文本来写文本吗?,android,android-edittext,Android,Android Edittext,如果文本框中写的文本等于12,我想干杯。 我使用了editText1.getText().toString()方法,但它似乎不起作用。 发生了什么?请检查public void tikla()方法好吗?Thx.代码中的这种比较是不正确的 public class RenkKorluguTesti extends Activity { /** Called when the activity is first created. */ ImageView imageView1; EditT

如果文本框中写的文本等于12,我想干杯。 我使用了editText1.getText().toString()方法,但它似乎不起作用。
发生了什么?请检查public void tikla()方法好吗?Thx.

代码中的这种比较是不正确的

  public class RenkKorluguTesti extends Activity {
  /** Called when the activity is first created. */

ImageView imageView1;
EditText editText1=null;
Button button1;
@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.galeri);

    imageView1 = (ImageView) findViewById(R.id.imageView1);
    editText1 = (EditText) findViewById(R.id.editText1);
    button1 = (Button) findViewById(R.id.button1);


    Gallery gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            switch(position) {

            case 0:{
                imageView1.setImageResource(R.drawable.plate01); 
                break;
            }
            case 1: imageView1.setImageResource(R.drawable.plate02); break;
            case 2: imageView1.setImageResource(R.drawable.plate03); break;

            }
            //Toast.makeText(RenkKorluguTesti.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });




   }

public void tikla(View v) {
    if(editText1.getText().toString()=="12")
        editText1.setText("yov");
        //Toast.makeText(RenkKorluguTesti.this, "Birinci testi geçtiniz.",Toast.LENGTH_SHORT).show();


}

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.plate01,
            R.drawable.plate02,
            R.drawable.plate03,

    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.RenkKorluguTesti);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.RenkKorluguTesti_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return mImageIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}




 }

比较字符串是通过not
==
运算符完成的。

在Java上,您不能进行这种比较:

(editText1.getText().toString()=="12")
您需要执行以下操作:

if(editText1.getText().toString()=="12")


您可以这样做:

if(editText1.getText().toString().compareTo("12")==0)

使用该方法。

我还想在if语句中添加一个条件,我该如何做呢?.public void tikla(View v){if(editText1.getText().toString().equals(“12”)/*&如果选择了库中的第一个图像*/)Toast.makeText(RenkKorluguTesti.this,“Birinci testi geçtiniz.”,Toast.LENGTH_SHORT.show()}if(editText1.getText().toString().equals(“12”)&&pathImage1!=null)但是您需要一个字符串变量(pathImage1)来存储所选图像的路径。我在您的代码示例中没有看到这一点。谢谢,但是,我想获取所选位置号,而不是路径。您知道如何才能获取它吗?@FigenGungor您想获取位置号吗(索引)图库中的图像?如果是这样,请参阅本文中的getLastImageId方法:我还想在我的If语句中添加一个条件,我该怎么做?.public void tikla(View v){If(editText1.getText().toString().equals(“12”)/*&如果选择了图库中的第一个图像*/)Toast.makeText(RenkKorluguTesti.this,“Birinci testi geçtiniz.”,Toast.LENGTHçSHORT).show()}我还想在我的if语句中再添加一个条件,我该如何做呢?.public void tikla(View v){if(editText1.getText().toString().equals(“12”)/*&如果库中的第一幅图像被选中*/)Toast.makeText(RenkKorluguTesti.this,“Birinci testi geçtiniz.”,Toast.LENGTHçSHORT).show();}
if(editText1.getText().toString().compareTo("12")==0)
if (editText1.getText().toString().compareTo("12") == 0)