第七篇 ScrollView控件
16lz
2021-01-23
直奔主题~!
结构如图:
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. */@Overridepublic 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 stubmyTast mt = new myTast(Control_ScrollViewActivity.this);mt.execute(httpaddress.getText().toString().trim());}};class myTast extends AsyncTask<String, Integer, String> {@Overrideprotected void onCancelled() {// TODO Auto-generated method stubsuper.onCancelled();}@Overrideprotected void onPostExecute(String result) {// TODO Auto-generated method stub// super.onPostExecute(result);result_txt.setText(result);pd.dismiss();}@Overrideprotected void onPreExecute() {// TODO Auto-generated method stub// super.onPreExecute();result_txt.setText("正在获取信息......");}@Overrideprotected 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 stubdialog.cancel();}});pd.setOnCancelListener(new DialogInterface.OnCancelListener() {public void onCancel(DialogInterface dialog) {// TODO Auto-generated method stubfinish();}});pd.show();}@Overrideprotected 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;}}}
更多相关文章
- Android改变图片颜色的自定义控件(十)
- android控件 ToggleButton的应用
- [置顶] android studio 如何进行格式化代码 快捷键必备
- android纯Java代码写的addView弹窗(亲测可用,欢迎留言)
- 整理的Android多种控件步骤
- 第四篇 Gallery控件
- android 代码设置apn