Android学习笔记5:常用控件(1)

来源:互联网 发布:怎么修改网络游戏数据 编辑:程序博客网 时间:2024/06/06 05:41

常用控件:

<!--
    android:id  —— 为控件指定相应的ID
    android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
    android:grivity —— 指定控件的基本位置,比如说居中,居右等位置
    android:textSize —— 指定控件当中字体的大小
    android:background —— 指定该控件所使用的背景色,RGB命名法 
    android:width —— 指定控件  的宽度
    android:height —— 指定控件的高度
    android:padding —— 指定控件的内边距,也就是说控件当中的内容(两控件包含关系)
    android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示

  android:layout_marginLeft    两个相邻控件的外边距,此处是与左边控件的距离(两控件平等关系)
     -->


1.TextView   它的常用属性如上面概括的

2.EditText  

增加一项常见属性     android:inputType="textPassword"   规定输入类型

 

3.Button

4.RadioButton  单选按钮

   几个单选按钮应该在布局文件中写成RadioGroup的子标签,而长和宽的属性一般设置为                                               android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

监听器与Button不同,

 //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("famale");Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}else if(maleButton.getId() == checkedId){System.out.println("male");}}});

5.多选按钮 CheckBox

//为多选按钮添加监听器        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("swim is checked");}else{System.out.println("swim is unchecked");}}});


6.Toast  悬浮窗口

Toast.makeText(this, "默认Toast样式",Toast.LENGTH_SHORT).show();

注意这里的This要写 当前要显示的Activity

7.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"
/>

使用方法:

setVisibility(View.VISIBLE)   //设置可见状态,DONE 为不可见

setMax(150)    // 设置进度条的最大值

setProgress(i)   //设置主进度条的值

setSecondaryProgress(i )    //设置副进度条的值


8.ListView  列表

①单行显示 :先设定整体布局文件main.xml , 然后每一行就用系统自带的布局文件android.R.layout.simple_list_item_1

public class ArrayListDemo extends Activity {     private ListView listView;     private String[] adapterData;      /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.array_list_layout);          /* 找到这个listView */         listView = (ListView) findViewById(R.id.array_list);          /* 我们要在listView上面没条显示的数据,放到一个数组中 */         adapterData = new String[] { "Afghanistan", "Albania", "Algeria",                 "American Samoa", "Andorra", "Angola", "Anguilla",                 "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia",                 "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",                 "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",                 "Benin", "Bermuda", "Bhutan", "Bolivia",                 "Bosnia and Herzegovina", "Botswana", "Bouvet Island" };                         /* 设置ListView的Adapter */ 
                  listView.setAdapter(new ArrayAdapter<String>(this,                   android.R.layout.simple_list_item_1, adapterData));     } } 
②每行显示两个信息,用键值对的形式,那么要继承自ListActivity,此时不需要整体布局文件,只需要每行ListView的布局文件

    一般就用SimpleAdapter这个适配器,SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义 好的视图。你可以指定数据支持的列表如ArrayList组成的Map。在ArrayList中 的每个条目对应List中的一行。Maps包含每行数据。你可以指定一个定义了被用 于显示行的视图XML文件,通过关键字映射到指定的视图。

构造函数:

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

里面5个参数,官方说明了其各个参数含义,我这里根据自己的理解解释下:

第一个context,很明显大家根据英文可以知道是上下文的意思,它官方的意思是:SimpleAdapter所要运行关联到的视图,这个是什么呢?就是你这个SimpleAdapter所在的Activity(一般而言),所以这个参数一般是this

第二个是一个泛型只要是一个List就行,这一般会想到是ArrayList,而他内部存储的则是Map或者继承自Map的对象,比如HashMap,这些语法都是Java的基本语法,不再详述了!这里呢是作为数据源,而且每一个ArraList中的一行就代表着呈现出来的一行,Map的键就是这一行的列名,值也是有列名的。

第三个资源文件,就是说要加载这个两列所需要的视图资源文件,你可以左边一个TextView右边一个TextView,目的在于呈现左右两列的值!

第四个参数是一个数组,主要是将Map对象中的名称映射到列名,一一对应

第五个是将第四个参数的值一一对象的显示(一一对应)在接下来的int形的id数组中,这个id数组就是LayOut的xml文件中命名id形成的唯一的int型标识符

public class Activity01 extends ListActivity {    /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);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("name", "zhangsan");map1.put("age", "24");map2.put("name", "zhangsan");map2.put("age", "25");map3.put("name", "wangwu");map3.put("age", "30");list.add(map1);list.add(map2);list.add(map3);SimpleAdapter adapter = new SimpleAdapter(Activity01.this, list,R.layout.user, new String[] { "name", "age" },new int[] { R.id.name1,R.id.age1});setListAdapter(adapter);}
    在创建的Activity如果继承自ListActivity类,则其默认拥有一个Listview控件提供使用, 下面主要就Listview中,item点击事件和长按事件的进行说明。1、首先获得listview实例;[java] view plaincopyprint?ListView lv = getListView();  2、添加点击事件  对lv添加点击监听器。[java] view plaincopyprint?lv.setOnItemClickListener(new OnItemClickListener() {              public void onItemClick(AdapterView<?> parent, View view,                      int position, long id) {                  // When clicked, show a toast with the TextView text                  TextView tv = (TextView)view.findViewById(R.id.textViewVideoTitle);                  Toast.makeText(getApplicationContext(),                          tv.getText(), Toast.LENGTH_SHORT).show();              }          });  其中R.id.textViewVideoTitle 是Listview,item中的一个TextView控件。3、添加长按事件[java] view plaincopyprint?lv.setOnItemLongClickListener(new OnItemLongClickListener(){              @Override              public boolean onItemLongClick(AdapterView<?> arg0, View arg1,                      int arg2, long arg3) {                  // TODO Auto-generated method stub                  // When clicked, show a toast with the TextView text                  TextView tv = (TextView)arg1.findViewById(R.id.textViewVideoTitle);                  Toast.makeText(getApplicationContext(),                          tv.getText(), Toast.LENGTH_SHORT).show();                  return false;              }          });  备注:对于缺少的包,在eclipse中按Ctrl+shift+O就可以自动引入。
原创粉丝点击