Android自学笔记(3)
2015-03-10 23:09
197 查看
几种常用控件的简单用法
1. TextView的简单用法
TextView控件就是App中那些用来显示字符串的控件,要对其内容进行编辑,首先就需要获取到TextView控件对象,首先在布局文件里面创建一个TextView布局<TextView android:id="@+id/factorTwo" android:layout_width="fill_parent" android:layout_height="wrap_content" />
这里其几个属性:
android:id是指这个控件的id,这里指的是factorTwo,当指定之后,在R.java文件中也会自动生成这个id地址
android:layout_width是指这个控件的宽度,这里指的是让其填满其父标签的对象(有可能是控件也有可能是布局)
android:layout_height这个指的是控件的高度,值于android:layout_width属性一样
当指定好id后就可以再activity里面获取到了
首先要在activity里面创建一个TextView对象
private TextView factorTwo;
之后利用activity对象的findViewById(String id)方法获取这个控件
factorTwo = (TextView)findViewById(R.id.factorTwo);
之后就可以对这个对象进行编辑操作了,这里编辑个最简单的,设置一下里面显示的问题的内容
factorTwo.setText(R.string.chengyi);
R对象里已经有我们注册过的字符串,所以这么取,之后运行后TextView控件中的内容已经被改变了
2.EditText简单用法
EditText就是用来获取用户输入的字符串的控件,当用户输入完字符串以后,可以将用户输入的内容传递给程序进行处理同样,首先要在布局文件中创建一个EditText控件
<EditText android:id="@+id/factorThree" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:labelFor="@id/factorThree" />
同样,在activity中也需要声明和获取
private EditText factorThree; factorThree = (EditText)findViewById(R.id.factorThree);
之后便可以取到控件中的字符串的值了
String factorThreeStr = factorThree.getText().toString();
3.Button的简单用法
Button就是页面上那些现实的按钮,点击之后可以实现一些相应的功能首先,布局文件中创建一个Button控件
<Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" />
之后获取控件对象
private Button button; button = (Button)findViewById(R.id.button);
对按钮里面所显示的字进行编辑
button.setText(R.string.jisuan);
之后设置按钮点击的监听器
button.setOnClickListener(new CaculateListener());
这里设置的监听器是一个类,这个类要实现OnClickListener接口,同时要覆写OnClickListener中的onclick方法,而这个方法就是当点击按钮之后触发的方法,里面可以编辑自己所需要实现的功能
class CaculateListener implements OnClickListener{ @Override public void onClick(View v) { } }
这里个这个类可以重新定义一个,也可以写成临时的内部类,如下:
button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } });
4.ListView的简单用法
ListView控件有点像平时用的通讯录那种的控件,有一个列表里面显示许多的item,这些item都可以点击效果如下:
首先,同样的,在layout文件中创建一个ListView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/listLinerLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollbars="vertical"/> </LinearLayout> </LinearLayout>
有关这里的android:drawSelectorOnTop=”false”指的是当点击listview上面的item的时候,会显示深颜色,而这个就是用来指定这个颜色是不是要把这条item上的文字遮挡住的设置,当设置成false的时候就不会遮住这个文字
下面要在activity中编辑这个控件了,创建一个activity用来显示这个listview
package com.dan; import java.util.ArrayList; import java.util.HashMap; import com.my.R; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; public class ListViewTest 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> map1 = new HashMap<String, String>(); HashMap<String, String> map2 = new HashMap<String, String>(); HashMap<String, String> map3 = new HashMap<String, String>(); map1.put("user_name", "zhangsan"); map1.put("user_id", "192.168.0.1"); map2.put("user_name", "lisi"); map2.put("user_id", "192.168.0.2"); map3.put("user_name", "wangwu"); map3.put("user_id", "192.168.0.3"); list.add(map1); list.add(map2); list.add(map3); 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); } }
这里需要注意的是这个显示ListView的Activity其继承的类是ListActivity而不是简单的Activity,其是专门为ListView设计的一个类。
当然很容易看出来,创建的三个Map便是要在ListView中显示的内容,之后都把他们存在了一个List里面
最复杂的就是这个这个SimpleAdapter的构造了,其构造函数一共有5个参数
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
这是他的函数原型,说明一下这些参数都是什么意思
context 指的就是这个listview的上下文,其实就是指的这个Activity,所以我们这里给他赋值为this将ListViewTest 这个Activity赋值上去
List<? extends Map<String, ?>> data这个指的是我们存放了需要显示的数据的键值对的list,一组数据(放在一个item中)对应一个Map
int resource 这里我们赋值的是另外一个layout的id,先贴上这个layout的代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingBottom="1dip"> <TextView android:id="@+id/user_name" android:layout_width="180dip" android:layout_height="wrap_content" android:textSize="10pt" android:singleLine="true"/> <TextView android:id="@+id/user_id" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="10pt" android:gravity="right"/> </LinearLayout>
很容易就看出,这个布局文件指的就是我们需要显示的内容在一条item中的布局,当然这里两个都用的是TextView用来显示一些字符串内容,其实也可以更换其他的控件来使用
String[] from 这里的字符串数组中存放是我们需要显示的内容Map对应的键的值
int[] to 这里的整型数组存放的是上面那组键的值所对应的内容需要显示在哪一个控件里面
new String[]{"user_name", "user_id"}, new int[]{R.id.user_name,R.id.user_id}
其实指的就是对于一个item来说,我的Map中的以”user_name”为键的对应的值域的值要让他显示在R.layout.user这个对应的LayOut中的以R.id.user_name为ID的控件中也就是这个控件中
<TextView android:id="@+id/user_name" android:layout_width="180dip" android:layout_height="wrap_content" android:textSize="10pt" android:singleLine="true"/>
最后再调用
setListAdapter(listAdapter)便完成了对ListView的创建
5.ProgressBar的用法
ProgressBar指的就是用来显示进度条的一个控件,样式如下:一种是以长条的形式显示的,一种是以旋转的圆圈显示的,以长条显示的这用进度条可以显示出具体的进度进行情况,而圆圈型的就不行
首先,还是要在layout中创建这两种ProgressBar控件
<ProgressBar android:id="@+id/firstBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <ProgressBar android:id="@+id/secondBar" style="?android:attr/progressBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
这里的style属性指的就是不同风格的ProgressBar样式
之后我们创建Activity对这两个控件进行操作
package com.dan; import com.my.R; 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 ProgreessBarTest extends Activity{ //声明变量 private ProgressBar firstBar = null; private ProgressBar secondBar = null; private Button beginButton = null; int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progreessbar); //根据控件ID取得代表控件的对象 firstBar = (ProgressBar)findViewById(R.id.firstBar); secondBar = (ProgressBar)findViewById(R.id.secondBar); beginButton = (Button)findViewById(R.id.beginButton); beginButton.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ @Override public void onClick(View arg0) { if(i == 0){ firstBar.setVisibility(View.VISIBLE); secondBar.setVisibility(View.VISIBLE); }else if (i < 100){ //设置主进度条的当前值 firstBar.setProgress(i); //设置第二进度条的当前值 firstBar.setSecondaryProgress(i + 10); System.out.println("enter"); //默认的进度条是不能显示进度的 //secondBar.setProgress(i); }else{ firstBar.setVisibility(View.GONE); secondBar.setVisibility(View.GONE); } i = i + 10; } } }
这里实现的功能是当点击按钮之后,进度条就会向前走,那么这时候只需要调用这个进度条对象的setprogress方法就可以对进度条的前进的百分比进行设置了,当然这里默认的进度条的最大值是100,如果需要设置成其他值,则在布局文件中设置android:max属性值即可
相关文章推荐
- 【Android自学笔记】android实现TextView垂直滚动显示
- 【Android自学笔记】android真机测试时 出现 “远程主机强迫关闭了一个现有的连接”的解决方法
- Android自学笔记-3-ADB的常用命令
- Android自学笔记-9-Pull方式处理XML
- Android自学笔记-8-Android数据存储1:文件、SharedPreferences和获取sd卡信息
- android自学笔记(一)
- Android自学笔记-5-click事件的四种写法
- Android自学笔记-12-ContentProvider简单例子
- Android自学笔记-1-android运行时Dalvik和art简单介绍
- Android自学笔记-10-Sqlite的简单使用
- 【Android自学笔记之三】surfaceView更新线程
- 【Android自学笔记】为Android应用程序添加Rate功能
- 【Android自学笔记】Android中的Log小技巧
- Android自学笔记-2-DDMS的简单介绍
- Android自学笔记之短信发送器
- 【Android自学笔记】Android获取手机和存储卡上的图片
- 【Android自学笔记】对应资源文件夹中的图标尺寸
- Android自学笔记:Tasks and Back Stack原理
- Android自学笔记-7-Android中的junit
- Android自学笔记 第一天