安卓四大核心组件指的是Activity、Service、BroadcastReceiver、ContentProvider。下面总结一下Activity中一些常用控件的用法。
主要有以下5个方面:
Part A:TextView和EditText的使用
Part B:ImageView的使用
Part C:ToggleButton(开关按钮)、RadioButton(单选按钮)和CheckBox(复选按钮)的使用
PartD:DatePicker和TimePicker的使用
PartE: ListView的使用
Part A:TextView和EditView的使用
图一
TextView(文本框)是Android系统中最常见的控件之一,使用TextView可生成一段文本文字,合理使用TextView的属性还能使文字变得有资有色。TextView还可以用来显示超链接、走马灯效果…
TextView控件可以通过XML文件设置全部属性,也可以通过java代码设置属性。
XML文件:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="@string/back"
android:textColor="#00ff00"
android:textSize="20sp"
/>
Java代码:
//获取TextView组件
TextView text = (TextView)findViewById(R.id.text);
//网页标签,这里一定要加http
String label = "<html><a href='http://www.baidu.com'>百度</a></html>";
//调用set方法设置属性
text.setTextSize(30);//设置文本的字体大小为30dp
text.setText(Html.fromHtml(label));//将网页标签显示出来
//设置移动方法,即超链接的跳转效果
text.setMovementMethod(LinkMovementMethod.getInstance());
EditText是Android系统中的编辑框,可以理解为可编辑的TextView,由图一亦可知EditText实为TextView的直接子类,故它的用法和属性设置与TextView很相似。但EditText的用途却比TextView多很多。以下是笔者的一些小总结:
XML文件:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
/>
Java代码:
EditText edit = (EditText)findViewById(R.id.edit);
//1.用于提示信息
String content = edit.getText().toString();
if(content == null||content.equals("")){
edit.setError("输入不能为空!!!");
}
2.
对要编辑的内容进行限定,如果没有
digits
、
inputType
、
numeric
这些限定,则默认为什么内容都能输入(数字、字母、符号)
XML文件:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="abczf"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapCharacters|number"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="decimal|signed"
/>
Part B:ImageView的使用
很多时候我们不想把东西都放在APK里面,或者是不能放进去,这时候我们就需要万能的网络帮助自己实现。
Java代码:
package com.example.test_widget;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
* ImageViewTest 从网络获取资源
* @author 赵芳
* 2014-7-18
* 下午5:04:24
*/
public class ImageViewTest extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
Button btn = new Button(this);
btn.setText("从网络获取图片");
final ImageView image = new ImageView(this);
layout.addView(btn);
layout.addView(image);
setContentView(layout);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Thread(){
public void run() {
try {
//1.根据流对象获取网络资源(1和2选其一)
InputStream in = getImageFromNet1("http:// 192.168.0.15:8080/test/test.jpg");
final Bitmap bmp = BitmapFactory.decodeStream(in);
//2.根据数组获取流对象
// byte[] by = getImageFromNet("http://192.168.0.15:8080/test/test.jpg");
// final Bitmap bmp = BitmapFactory.decodeByteArray(by, 0, by.length);
image.post(new Runnable() {
public void run() {
image.setImageBitmap(bmp);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
});
}
/**
* 从指定的url上获取资源
* @param path 指定的url
* @return 流对象
*/
public InputStream getImageFromNet1(String path){
InputStream in = null;
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
in = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return in;
}
/**
* 根据路径path从网络上获取图片
* @param path
* @return 字节数组对象
* @throws MalformedURLException
*/
public byte[] getImageFromNet(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
byte[] by = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = -1;
while((len = in.read(by)) != -1)
{
baos.write(by, 0, len);
}
return baos.toByteArray();
}
}
Part C:ToggleButton(开关按钮)、RadioButton(单选按钮)和CheckBox(复选按钮)的使用
参考链接:http://www.cnblogs.com/plokmju/archive/2013/07/22/android_UI_CompoundButton.html
PartD:DatePicker和TimePicker的使用
Toggle.xml文件:<DatePicker
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<TimePicker
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
java代码:
package com.example.test_widget;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
/**
* 设置时间日期
* @author
* 2014-7-20
* 下午6:10:12
*/
public class DataPickerTest extends Activity {
private DatePicker date;
private TimePicker time;
private int year,month,day,hour,min;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toggle);
date = (DatePicker)findViewById(R.id.date);
date.setCalendarViewShown(false);//若版本较低,此处会报错
time = (TimePicker)findViewById(R.id.time);
time.setIs24HourView(true);//設置24小時制
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
//监听date、time
date.init(year, month, day, new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
//將改变的時间显示到activity标题上
DataPickerTest.this.year = year;
month = monthOfYear + 1;
day = dayOfMonth;
setTitle(DataPickerTest.this.year+"-"+month+"-"+day+" "+hour+":"+min);
}
});
time.setOnTimeChangedListener(new OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
hour = hourOfDay;
min = minute;
}
});
}
}
PartE: ListView的使用
在Android开发中,ListView是比较常用的控件,它以列表的形式显示具体内容,并且能够根据数据的长度自适应显示。 在ListView中可以根据需要显示自定义的列表内容,包括文字(TextView)、图片(ImageView)、按钮(Button)等,以此构成图文并茂的显示效果。
Java代码:
package com.example.test_widget;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
/**
* listview测试类,布局文件限制条目的改变( 数据源----->适配器------>界面)
* 主要布局文件:list.xml 另外还有个适配器的关联文件:list_item.xml
* @author 赵芳 2014-7-20 下午9:28:52
*/
public class ListViewTest extends Activity {
private ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
list = (ListView) findViewById(R.id.list);
// 关联数据源
final List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
// 模拟数据源
for (int i = 0; i < 10; i++)
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "title" + i);
map.put("content", "content" + i);
map.put("image", R.drawable.face2);
dataList.add(map);
}
// 构建适配器
SimpleAdapter adapter = new SimpleAdapter(this, dataList,
R.layout.list_item,
new String[] { "title", "content", "image" }, new int[] {
R.id.title, R.id.content, R.id.image });
list.setAdapter(adapter);
// 监听list中每个条目被点击
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 获取选中条目的title
// setTitle((String)dataList.get(position).get("title"));
TextView content = (TextView)view.findViewById(R.id.content);
setTitle(content.getText());
}
});
// 监听list中每个条目被长时间点击
list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ListViewTest.this, "你选择了第" + position + "条目", 3).show();
return false;
}
});
}
}
List.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/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
List_item.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginLeft="10sp"
android:id="@+id/title" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20sp"
android:layout_marginTop="20sp"
android:id="@+id/image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_below="@id/title"
android:id="@+id/content" />
</RelativeLayout>
更多参考链接:http://www.cnblogs.com/menlsh/archive/2013/03/15/2962350.html