Android的View 简单理解和实例
2014-08-26 22:41
232 查看
1.View的基本概念
在Activity显示的控件 都叫做View(View类 是所有的控件类的父类 比如 文本 按钮)
2.在Activity当中获取代表View的对象
Activity读取布局文件生成相对应的 各种View对象
TextView textView=(TextView)findViewBy(R.id.textView)
3.设置view的属性
Activity_mian.xml 这样的xml布局文件中发现了,类似@+id/和@id/到底有什么区别呢? 这里@可以理解为引用,而多出的+代表自己新声明的
4.为View设置监听器
一个控件可以绑定多个监听器 不通过的监听器响应不同的事件
获取代表控件的对象
定义一个类,实现监听接口 implements OnClickListener
生成监听对象
为控件绑定监听对象
[html] view
plaincopy
改成垂直布局
布局文件
<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_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="80px"
android:background="#FF0000"
android:text="hello_world 熊" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击"/>
</LinearLayout>
[java] view
plaincopy
MianActivity文件
package com.xiong.fisrt_android;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
textView.setText("hello Android!!!");
textView.setBackgroundColor(Color.BLUE);
ButtoneListener buttoneListener = new ButtoneListener();// 生成监听对象
button.setOnClickListener(buttoneListener);// 按钮绑定一个监听器
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ButtoneListener implements OnClickListener// 创建一个类实现监听事件的接口
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count++;
textView.setText(Integer.toString(count));
}
}
}
在Activity显示的控件 都叫做View(View类 是所有的控件类的父类 比如 文本 按钮)
2.在Activity当中获取代表View的对象
Activity读取布局文件生成相对应的 各种View对象
TextView textView=(TextView)findViewBy(R.id.textView)
3.设置view的属性
Activity_mian.xml 这样的xml布局文件中发现了,类似@+id/和@id/到底有什么区别呢? 这里@可以理解为引用,而多出的+代表自己新声明的
4.为View设置监听器
一个控件可以绑定多个监听器 不通过的监听器响应不同的事件
获取代表控件的对象
定义一个类,实现监听接口 implements OnClickListener
生成监听对象
为控件绑定监听对象
[html] view
plaincopy
改成垂直布局
布局文件
<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_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="80px"
android:background="#FF0000"
android:text="hello_world 熊" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击"/>
</LinearLayout>
[java] view
plaincopy
MianActivity文件
package com.xiong.fisrt_android;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
textView.setText("hello Android!!!");
textView.setBackgroundColor(Color.BLUE);
ButtoneListener buttoneListener = new ButtoneListener();// 生成监听对象
button.setOnClickListener(buttoneListener);// 按钮绑定一个监听器
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ButtoneListener implements OnClickListener// 创建一个类实现监听事件的接口
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
count++;
textView.setText(Integer.toString(count));
}
}
}
相关文章推荐
- Android的View 简单理解和实例
- Android中视图View的深入理解及实例演示——以腾讯笔试题为例
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- Android 简单易理解的ViewPager嵌套Fragment展示页面滑动效果
- Android自定义ViewGroup(侧滑菜单)详解及简单实例
- Android View事件分发和消费源码简单理解
- Android中将View的内容保存为图像的简单实例
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能实例
- Android中SurfaceView用法简单实例
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- android的WebView简单实例
- Webview实现android简单的浏览器实例代码
- Android学习历程15-SurfaceView的简单用法(实例:幸运抽奖以及控制中奖物品)
- 学习笔记--android中surfaceview 简单用法实例:画板。
- Android WebView 详解及简单实例
- Android-->ViewPager setPageTransformer的简单理解
- android 自定义view的简单实例
- Android 中 ViewFlipper 的简单实例
- Android的使用ViewPager简单实例【类似tab翻页】
- 理解Android Web Apps的运行机制,实现简单的包含Web View的应用程序