我有錯誤代碼,幫我解決這個問題,讓文字網址從EditText上點擊按鈕後顯示在ImageView的圖像,後輸入文本URL,然後按按鈕顯示圖像使用
MainActivity.java
public class MainActivity extends Activity {
Button bt;
EditText et;
ImageView iv;
Runnable stream;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadGambar();
stream = new Runnable() {
@Override
public void run() {
try {
loadGambar();
} catch (Exception e) {
e.getStackTrace();
}
new Handler().postDelayed(stream, 2);
}
};
new Handler().postDelayed(stream, 2);
}
private void loadGambar(){
try {
iv = (ImageView) findViewById(R.id.imageView1);
et = (EditText) findViewById(R.id.editText1);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new view.OnClickListener() {
public void onClick(View view) {
et = (EditText)findViewById(R.id.editText1);
iv = (ImageView)findViewById(R.id.imageView1);
iv.setBackground(et.getText().toString());
}
});
URL url = new URL (et.getText().toString());
InputStream is = (InputStream) url.getContent();
Drawable gambar = Drawable.createFromStream(is, "src");
iv.setImageDrawable(gambar);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
acitvity_main.xml
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:ems="10" android:inputType="textUri">
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="21dp"
android:text="@string/button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="18dp"
android:src="@drawable/ic_action_search" android:contentDescription="@string/app_name"/>
我怎麼能inpu後顯示t URL編輯文本並按下按鈕以顯示來自URL的圖像
你得到什麼錯誤?你能簡單地告訴你想要達到的目標嗎? – Rahul
我在bt.setClickOnlistener上出錯, –
你得到了什麼錯誤。粘貼您的Logcat跟蹤 – Rahul