View初步(一)

在此之前装上第三方Android设备模拟器Genymobile, 自带的太慢了.....

过程如下:http://www.cnblogs.com/iMirror/p/3768533.html

1. View的基本概念

2. 在Activity当中获取代表View的对象

3. 设置View的属性

4. 为View设置监听器

1. View的基本概念

在Activity上显示的所有控件就叫View, 都是用对象表示的, 生成对象的类都是View的子类

View是所有控件类的父类

2. 在Activity当中获取代表View的对象

布局文件 fragment_main.xml


 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      //线性布局
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical"
6 android:paddingBottom="@dimen/activity_vertical_margin"
7 android:paddingLeft="@dimen/activity_horizontal_margin"
8 android:paddingRight="@dimen/activity_horizontal_margin"
9 android:paddingTop="@dimen/activity_vertical_margin"
10 tools:context="first.pack.MainActivity$PlaceholderFragment" >
11
12 <TextView
13 android:id="@+id/textView"
14 android:layout_width="match_parent"
15 android:layout_height="wrap_content"
16 android:background="#FF0000" //红色
17 android:text=" Hello_World" />
18
19 </LinearLayout>

在MainActivity.java中, 修改onCreate函数


 1 private TextView textView;    //生成名为textView的对象
2 @Override
3 protected void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 setContentView(R.layout.activity_main);
6 if (savedInstanceState == null) {
7 getSupportFragmentManager().beginTransaction()
8 .add(R.id.container, new PlaceholderFragment())
9 .commit();
10 }
11 textView = (TextView)findViewById(R.id.textView); //获取该id返回值是View类型并向下转型
12 textView.setText("Hello Mirror");
13 }

目前这里还未调试通过!

3. 设置View的属性

既可在布局文件中设置, 也可在代码中设置.

4. 为View设置监听器

控件          女生

监听器      
男朋友    不同监听器 为控件 执行不同工作

使用步骤:

监听器是一个对象!!!!监听器的使用方法基本思路都是如下!!!

在fragment_main.xml中


 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical"
6 android:paddingBottom="@dimen/activity_vertical_margin"
7 android:paddingLeft="@dimen/activity_horizontal_margin"
8 android:paddingRight="@dimen/activity_horizontal_margin"
9 android:paddingTop="@dimen/activity_vertical_margin"
10 tools:context="first.pack.MainActivity$PlaceholderFragment" >
11
12 <TextView
13 android:id="@+id/textView"
14 android:layout_width="match_parent"
15 android:layout_height="wrap_content"
16 android:background="#FF0000"
17 android:text= "0" />
18
19 <Button
20 android:id="@+id/button"
21 android:layout_width="match_parent"
22 android:layout_height="wrap_content"
23 android:text= "button"
24 />
25
26 </LinearLayout>

在MainActivity中, 注意几点:

1. 导入TextView 类, 选中 并 快捷键
ctrl+shift+O

导入Button类也是的

2.
在实现OnclickListener之前导入android.view.View.OnClickListener;

点击左边的红色小叉叉, 弹出解决方案, 选择第一个Add unimplemented method 复写接口的方法

时间: 2024-08-05 06:00:14

View初步(一)的相关文章

【Android 初学】2、View初步

1.View的基本概念 View就是所有控件类的父类.(文本.按钮.多选.布局.··· ···) 2.在Activity当中获取代表View的对象 使用findViewById(R.id.ppp) ;ppp表示控件的ID 例如TextView textView= (TextView)findViewById(R.id.textView); 该方法将返回一个View类型,必须使用向下转型为响应的控件. 3.设置View的属性 使用上一个textView的对象: 可以修改该控件的text属性,如:t

Android自定义View初步

有关使用Android如何设计出有个性的界面,按照本人估计,除了遵循google的设计规范,就只能使用自定义View这个最灵活的方式了,这几天找了些资料学习自定义View,但是学习android developer文档中自定义的View比较麻烦,又找了些比较简单的材料,结合自己对CustomView这个实例的理解,开始学习自定义View. 下面实现一个类似时钟/仪表盘的简单界面,通过绘制一个圆来实现,这个圆周围有标的刻度,同时在每五个位置上绘制一个比其他刻度线长的刻度,然后再绘制一个类似的表针.

View初步练习

activity_main.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_pa

【Android学习】《Android开发视频教程》第一季笔记

视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5    Activity基础概念 1.Android开发技术结构图 2.Android四大组件 Activity(页面).Service(后台运行的服务).Content Provider(数据提供者,向外暴露数据的方法,数据共享).Broadcast Receiver(监听手机发生的一切事情) 课时6    文本框与按钮的使用方法(Activity初步一) 1

Android 面试题总结之Android 基础(六)

Android 面试题总结之Android 基础(六) 在上一章节Android 面试题总结之Android 基础ListView(五) 主要是ListView的优化,原理以及一些基本问题. 在阅读过程中有任何问题,请及时联系.如需转载请注明 fuchenxuan de Blog 本章系<Android 之美 从0到1 – 高手之路>Android基础将会总结了Android 布局常见面试问题.其实对于基础方面Android 开发来说,经常面试无非就是UI,网络,数据库,这三大方面,本章节总结

好吧就让我们结束这一切吧

http://www.tudou.com/programs/view/K7lbU7LsiJI/HGN13.htmlhttp://www.tudou.com/programs/view/SfcF7r7DsCk/XCWU7.htmlhttp://www.tudou.com/programs/view/BbljqbN52ZY/ieGvM.htmlhttp://www.tudou.com/programs/view/R4thdEitDik/17gGr.htmlhttp://www.tudou.com/p

法涉法而尴尬的收入法国

http://www.tudou.com/programs/view/pQOO07vn4Sc/l99Fi.htmlhttp://www.tudou.com/programs/view/EDJkM6ojYkc/r7RA6.htmlhttp://www.tudou.com/programs/view/Gf0zVZygPtU/EmvXY.htmlhttp://www.tudou.com/programs/view/UhqFbgbyQ6k/Z23Lx.htmlhttp://www.tudou.com/p

我不要说谎好吗

http://www.tudou.com/programs/view/KhjSmQKHEzM/McEy9.htmlhttp://www.tudou.com/programs/view/5FNDTTRKi6Y/9719g.htmlhttp://www.tudou.com/programs/view/UWFf0dz2DEk/3xPj2.htmlhttp://www.tudou.com/programs/view/b2zsYr4dCZg/gv6FP.htmlhttp://www.tudou.com/p

我不要说谎

http://www.tudou.com/programs/view/KhjSmQKHEzM/McEy9.htmlhttp://www.tudou.com/programs/view/5FNDTTRKi6Y/9719g.htmlhttp://www.tudou.com/programs/view/UWFf0dz2DEk/3xPj2.htmlhttp://www.tudou.com/programs/view/b2zsYr4dCZg/gv6FP.htmlhttp://www.tudou.com/p