在我所见的任何地方,他们都使用带有 onClick 的线程。现在我有点被迫使用线程,因为我无法在其主线程上执行网络操作,如文档所述。那么我如何将线程放在我编写过这样的代码的按钮上?
public void firstbutton(View view)
{
//some code
}
谢谢您的帮助!
编辑:
public void firstbutton(View view)
{
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText editText = (EditText)findViewById(R.id.editText1);
inputMgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
EditText idnumber=(EditText)findViewById(R.id.editText1);
String idnumber2= idnumber.getText().toString();
int i = Integer.parseInt(editText.getText().toString());
idnum=i;
setContentView(R.layout.viewer);
Context context = view.getContext();
Drawable image = ImageOperations(context, WEB ADDRESS HIDDEN FOR PRIVACY"+idnumber2);
ImageView icon = new ImageView(context);
icon = (ImageView)findViewById(R.id.imageView1);
icon.setImageDrawable(image);
};
public Drawable ImageOperations(Context ctx, String url) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException
{
URL url = new URL(address);
Object content = url.getContent();
return content;
}