我是eclipse IDE上的android應用程序開發新手。Android:更改ImageView上的圖像Edittext消失
我想做一個基本的應用程序,其中,單擊按鈕時,ImageView中的圖像以及EditText中的文本分別替換爲另一圖像和文本。
但是這樣做,主要是當應用程序啓動時,頂部顯示一個按鈕,然後是ImageView,然後是EditText,它們工作正常,但點擊按鈕時,只有圖像被成功替換,而EditText消失。你能幫我解釋爲什麼會發生這種情況,以及如何糾正它?
XML文件:
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/click_it" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:src="@drawable/prac" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/imageView1"
android:layout_below="@+id/imageView1"
android:layout_marginTop="30dp"
android:ems="10"
android:text="hello!" >
<requestFocus />
</EditText>
</RelativeLayout>
的java文件:
package com.example.practicetwo;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
Button b1;
ImageView iv1;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(new R.id().button1);
iv1 = (ImageView)findViewById(new R.id().imageView1);
et1 = (EditText)findViewById(new R.id().editText1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv1.setImageResource(R.drawable.prac1);
et1.setText("how r u?");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
嘗試以靜態方式訪問,如findViewById(R.id.editText1),而不是新的findViewById(R.id()。editText1) – TMichel 2013-02-21 17:34:59
您的圖像prac1可能會大於屏幕的大小,並且當您替換prac編輯文本被壓下。嘗試使用「LinearLayout」而不是相對佈局。編輯上的 – Naveen 2013-02-21 17:38:34
,et1 =(EditText)findViewById(R.id()。editText1);它顯示錯誤。 – 2013-02-21 17:39:45