Android的View 简单理解和实例

本文介绍了Android应用开发的基础知识,包括如何使用View组件构建界面、设置属性及监听器等。通过实例展示了如何创建简单的应用程序,涉及Activity、TextView和Button等基本元素。
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 plain copy
  1. 改成垂直布局  
  2.   
  3. 布局文件  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.     android:orientation="vertical"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/textView"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:textSize="80px"  
  16.         android:background="#FF0000"  
  17.         android:text="hello_world 熊" />  
  18.       
  19.     <Button   
  20.         android:id="@+id/button"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="点击"/>  
  24.   
  25. </LinearLayout>  

[java]  view plain copy
  1. MianActivity文件  
  2. package com.xiong.fisrt_android;  
  3.   
  4. import android.app.Activity;  
  5. import android.graphics.Color;  
  6. import android.os.Bundle;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     private TextView textView;  
  16.     private Button button;  
  17.     private int count = 0;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         textView = (TextView) findViewById(R.id.textView);  
  24.         button = (Button) findViewById(R.id.button);  
  25.         textView.setText("hello  Android!!!");  
  26.         textView.setBackgroundColor(Color.BLUE);  
  27.         ButtoneListener buttoneListener = new ButtoneListener();// 生成监听对象  
  28.         button.setOnClickListener(buttoneListener);// 按钮绑定一个监听器  
  29.     }  
  30.   
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // Inflate the menu; this adds items to the action bar if it is present.  
  34.         getMenuInflater().inflate(R.menu.main, menu);  
  35.         return true;  
  36.     }  
  37.   
  38.     class ButtoneListener implements OnClickListener// 创建一个类实现监听事件的接口  
  39.     {  
  40.   
  41.         @Override  
  42.         public void onClick(View arg0) {  
  43.             // TODO Auto-generated method stub  
  44.             count++;  
  45.             textView.setText(Integer.toString(count));  
  46.   
  47.         }  
  48.   
  49.     }  
  50.   
  51. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值