直奔主题~!

结构如图:

main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout android:layout_width="fill_parent"
		android:id="@+id/linearLayout1" android:layout_height="wrap_content">
		<EditText android:id="@+id/address_txt" android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:layout_weight="1">
			<requestFocus></requestFocus>
		</EditText>
		<Button android:text="submti" android:id="@+id/submit_btn"
			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	</LinearLayout>
	<ScrollView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:id="@+id/scrollView1">
		<LinearLayout android:id="@+id/linearLayout2"
			android:layout_width="fill_parent" android:layout_height="fill_parent">
			<TextView  android:id="@+id/result"
				android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        </LinearLayout>
	</ScrollView>
</LinearLayout>

Control_ScrollViewActivity.java代码:

public class Control_ScrollViewActivity extends Activity {
	private EditText httpaddress;
	private Button submit_btn;
	private TextView result_txt;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findAll();
		bind();
	}

	public void findAll() {
		httpaddress = (EditText) this.findViewById(R.id.address_txt);
		submit_btn = (Button) this.findViewById(R.id.submit_btn);
		result_txt = (TextView) this.findViewById(R.id.result);
	}

	public void bind() {
		submit_btn.setOnClickListener(mylistener);
	}

	private View.OnClickListener mylistener = new OnClickListener() {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			myTast mt = new myTast(Control_ScrollViewActivity.this);
			mt.execute(httpaddress.getText().toString().trim());
		}
	};

	class myTast extends AsyncTask<String, Integer, String> {
		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			// super.onPostExecute(result);
			result_txt.setText(result);
			pd.dismiss();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			// super.onPreExecute();
			result_txt.setText("正在获取信息......");
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			// super.onProgressUpdate(values);
			pd.setProgress(values[0]);
		}

		ProgressDialog pd;

		public myTast(Context ct) {
			pd = new ProgressDialog(ct);
			pd.setCancelable(true);
			pd.setMax(100);
			pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			pd.setButton("cancel", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
			pd.setOnCancelListener(new DialogInterface.OnCancelListener() {

				public void onCancel(DialogInterface dialog) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			pd.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			// return null;
			try {

				HttpClient client = new DefaultHttpClient();
				HttpGet get = new HttpGet(arg0[0]);
				HttpResponse response = client.execute(get);
				HttpEntity entity = response.getEntity();
				long length = entity.getContentLength();
				InputStream is = entity.getContent();
				String s = null;
				if (is != null) {
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					byte[] buf = new byte[128];
					int ch = -1;
					int count = 0;
					while ((ch = is.read(buf)) != -1) {
						baos.write(buf, 0, ch);
						count += ch;
						if (length > 0) {
					        publishProgress((int) ((count / (float) length) * 100));
						}
					  Thread.sleep(10);
					}
					s = new String(baos.toByteArray());
				}
			  return s;
			} catch (Exception e) {
				e.printStackTrace();

			}

			return null;
		}

	}
}
posted on 2011-09-14 17:00  Jwc  阅读(364)  评论(0编辑  收藏  举报