我有错误代码帮助我修复它,单击按钮后从edittext获取文本URL并在imageView上显示图像,
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;
}
}
活动主文件.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"/>
我如何在输入 URL 到 edittext 并按下按钮以显示来自 URL 的图像后显示