<7.23>新的启航 Android学习笔记(三)

<7.22>新的启航 Android学习笔记(三)


开始时间:

9:30


今日目标:

1.继续学习android 5课

2.巩固Java高级技术

3.在虚拟机 mac 系统下将android开发环境搭建完成


加油!


第十二课:常用控件(二)


一、RadioGroup和RadioButton的使用方法

二、CheckBox的使用方法


三、Toast的基本用法


附练习代码:

MainActivity

package com.example.radiogroupandcheckbox;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private RadioGroup genderGroup = null;
	private RadioButton male = null;
	private RadioButton female = null;

	private CheckBox computer = null;
	private CheckBox run = null;
	private CheckBox basketball = null;

	private TextView show = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
		male = (RadioButton) findViewById(R.id.male);
		female = (RadioButton) findViewById(R.id.female);

		computer = (CheckBox) findViewById(R.id.computer);
		basketball = (CheckBox) findViewById(R.id.basketball);
		run = (CheckBox) findViewById(R.id.run);

		show = (TextView) findViewById(R.id.show);

		genderGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						if (male.getId() == checkedId) {
							Toast.makeText(MainActivity.this, "你的性别为:男",
									Toast.LENGTH_SHORT).show();
						} else if (female.getId() == checkedId) {
							Toast.makeText(MainActivity.this, "你的性别为:女",
									Toast.LENGTH_SHORT).show();
						}
					}
				});

		computer.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					show.setText(show.getText() + "电脑");
				}
			}
		});

		run.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					show.setText(show.getText() + "跑步");
				}
			}
		});

		basketball.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					show.setText(show.getText() + "篮球");
				}
			}
		});
	}

}

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/select_gender" />

    <RadioGroup
        android:id="@+id/genderGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male" />

        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/select_your_hobby" />

    <CheckBox
        android:id="@+id/computer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/computer" />

    <CheckBox
        android:id="@+id/basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/basketball" />

    <CheckBox
        android:id="@+id/run"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/run" />

    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>




第十三课:常用控件(三)


一、ProgressBar的使用方法

附练习代码:

MainActivity:

package com.example.progressbarandlistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

	private ProgressBar progressBar1 = null;
	private ProgressBar progressBar2 = null;
	private Button button1 = null;

	private int i = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
		progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);

		button1 = (Button) findViewById(R.id.button1);

		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (0 == i) {
					progressBar1.setVisibility(View.VISIBLE);
					progressBar2.setVisibility(View.VISIBLE);
				} else if (i < progressBar1.getMax()) {
					progressBar1.setProgress(i);
					progressBar1.setSecondaryProgress(i + 10);
				} else {
					progressBar1.setVisibility(View.GONE);
					progressBar2.setVisibility(View.GONE);
				}
				i += 10;
			}
		});
	}
}



二、ListView的使用方法


附练习代码:

package com.example.progressbarandlistview;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ListViewActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);

		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> user1 = new HashMap<String, String>();
		HashMap<String, String> user2 = new HashMap<String, String>();
		HashMap<String, String> user3 = new HashMap<String, String>();

		user1.put("user_name", "张三");
		user1.put("user_id", "1001");

		user2.put("user_name", "李四");
		user2.put("user_id", "1002");

		user3.put("user_name", "王五");
		user3.put("user_id", "1003");

		list.add(user1);
		list.add(user2);
		list.add(user3);

		SimpleAdapter listAdapter = new SimpleAdapter(this, list,
				R.layout.user, new String[] { "user_name", "user_id" },
				new int[] { R.id.user_name, R.id.user_id });

		setListAdapter(listAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Toast.makeText(this, "你点击的位置为:" + position, Toast.LENGTH_SHORT).show();
	}

}


listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" 
        android:drawSelectorOnTop="false">
    </ListView>

</LinearLayout>



第十四课:Handler的使用方法(一
一、Handler的基本概念
用它来处理一些耗时的任务,如:下载文件等,避免主界面无响应,影响用户体验

二、Handler基本使用方法

附练习代码:

package com.yrh.handleractivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button startButton = null;
	private Button endButton = null;
	private int a = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		startButton = (Button) findViewById(R.id.startButton);
		endButton = (Button) findViewById(R.id.endButton);

		startButton.setOnClickListener(new startButtonListener());
		endButton.setOnClickListener(new endButtonListener());
	}

	class startButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			handler.post(updateThread);
		}
	}

	class endButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			handler.removeCallbacks(updateThread);
			Toast.makeText(MainActivity.this, "测试结束", Toast.LENGTH_SHORT).show();
		}
	}

	Handler handler = new Handler();
	Runnable updateThread = new Runnable() {

		@Override
		public void run() {
			Toast.makeText(MainActivity.this, a + "", Toast.LENGTH_SHORT)
					.show();
			a += 1;
			handler.postDelayed(updateThread, 2000);
		}
	};

}




三、使用Handler更新ProgressBar


附练习代码:

package com.example.handlerandprogressbar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button downLoad = null;
	private ProgressBar progressBar = null;
	private TextView textView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		downLoad = (Button) findViewById(R.id.button1);
		progressBar = (ProgressBar) findViewById(R.id.progressBar);
		textView = (TextView) findViewById(R.id.textView);

		downLoad.setOnClickListener(new downLoadClickListener());
	}

	class downLoadClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			progressBar.setVisibility(View.VISIBLE);
			textView.setVisibility(View.VISIBLE);
			textView.setText("当前进度:0%");
			handler.post(updateThread);
		}
	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			progressBar.setProgress(msg.arg1);
			textView.setText("当前进度:" + msg.arg1 + "%");
			handler.post(updateThread);
		}
	};

	Runnable updateThread = new Runnable() {
		int i = 0;

		@Override
		public void run() {
			i += 5;
			Message msg = handler.obtainMessage();
			msg.arg1 = i;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			handler.sendMessage(msg);
			if (100 == i) {
				handler.removeCallbacks(updateThread);
				Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT)
						.show();
			}
		}
	};

}


第十五课:Handler的使用方法(二)


一、Handler与线程


二、Bundle的用法



三、在新线程当中处理消息的方法



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值