在此之前装上第三方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