为什么我不能用EditText编写文本?

时间:2012-02-22 13:59:10

标签: android android-edittext

  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;
    }
}




 }

如果在EditText框中写入的文本等于12,我想要Toast。 我使用了editText1.getText()。toString()方法,但它似乎无法正常工作。 怎么了?你能检查一下public void tikla()方法吗? THX。

3 个答案:

答案 0 :(得分:0)

您的代码中的此比较不正确。

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

比较字符串是通过equals()而不是==运算符完成的。

答案 1 :(得分:0)

在Java上你不能做这种比较:

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

你需要这样做:

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

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

答案 2 :(得分:0)

您可以这样做:

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

使用java.lang.String.compareTo(java.lang.String)方法。