安卓四大核心组件之Activity

2024-09-04 08:08

本文主要是介绍安卓四大核心组件之Activity,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

安卓四大核心组件指的是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文件:
<DatePickerandroid:id="@+id/date"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/><TimePickerandroid: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、timedate.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












这篇关于安卓四大核心组件之Activity的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1135467

相关文章

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

Linux find 命令完全指南及核心用法

《Linuxfind命令完全指南及核心用法》find是Linux系统最强大的文件搜索工具,支持嵌套遍历、条件筛选、执行动作,下面给大家介绍Linuxfind命令完全指南,感兴趣的朋友一起看看吧... 目录一、基础搜索模式1. 按文件名搜索(精确/模糊匹配)2. 排除指定目录/文件二、根据文件类型筛选三、时间

Spring组件初始化扩展点BeanPostProcessor的作用详解

《Spring组件初始化扩展点BeanPostProcessor的作用详解》本文通过实战案例和常见应用场景详细介绍了BeanPostProcessor的使用,并强调了其在Spring扩展中的重要性,感... 目录一、概述二、BeanPostProcessor的作用三、核心方法解析1、postProcessB

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Vue3中的动态组件详解

《Vue3中的动态组件详解》本文介绍了Vue3中的动态组件,通过`component:is=动态组件名或组件对象/component`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRa... 目录vue3动态组件动态组件的基本使用第一种写法第二种写法性能优化解决方法总结Vue3动态组件动态

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言