在上篇博文讲解了Android的Activity这个组件的启动流程后,接下来我们就来看看我们的Activity与我们的布局文件的关系吧

我们先来看看一个最简单的布局文件的内容:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="50sp"
android:background="#00DD00"
android:text="@string/hello_world" /> </RelativeLayout>

在这个布局文件里面,我们在里面定义了一个TextView控件,这个控件就是文本内容的显示,我们可以给其指定各种属性,例如高度、宽度、背景色等。在布局文件配置好以后,我们来看看Activity对象里面的onCreate()方法中的内容:

  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

我们看到 setContentView(R.layout.activity_main);这里我们就是加载了我们指定的那个布局文件,当执行完这句话以后,布局文件里定义的所有的控件都会被加载进来,并且生成对应的View对象,说到View对象,我们要知道,我们定义的所有的控件,例如TextView、Button等等,这些都是View对象的子类

我们知道,在布局文件被加载后,就会生成对应的控件对象,我们要如何在代码中得到该对象呢?可以通过 findViewById 这个方法,就可以根据ID找到我们需要的那个View对象了,例如我们要找到刚才的那个TextView对象:

    private TextView textView;

    @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView);
textView.setBackgroundColor(Color.GREEN);
   }

通过上面的 textView = (TextView) findViewById(R.id.textView);代码就可以得到我们布局文件中的TextView对象,因为所有的控件对象都是View的子类,而findViewById方法返回的是View类型的对象,所以我们这里要对其进行向下类型转换。在得到TextView这个对象后,我们就可以在java代码里来设置其各个属性。记住:在布局文件中能配置的内容,在java代码中也能设置,反之亦然

在了解了这些之后,我们再来学一个知识点: 监听器,相信做过java开发的都知道监听器的概念,所以这里也就不阐述了。我们这里要实现的是通过给一个Button控件注册监听器,来处理一些操作。

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="0" /> <Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button" /> </LinearLayout>

这是我们的布局文件,里面定义了一个TextView和一个Button,我们要做的就是当点击一下Button时,让TextView的内容每次加1,当长按Button时,让TextView的内容每次加2

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);
textView.setBackgroundColor(Color.GREEN); button = (Button) findViewById(R.id.button);
// 给button对象注册一个onClick监听器
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
count++;
textView.setText(count + "");
}
});
// 给button注册一个onLongClick监听器
button.setOnLongClickListener(new OnLongClickListener()
{
/**
* 这个方法返回一个boolean值,如果返回true,则表示是一个长按的操作,会执行下面这个方法
* 如果返回false,则表示是一个点击操作,会首先执行完下面的方法,然后再执行点击的方法
*/
@Override
public boolean onLongClick(View arg0)
{
count += 2;
textView.setText(count+"");
return true;
}
});
} @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;
} }

这个就是我们的Activity的代码,我们首先要获得TextView和Button这个对象,通过findViewById就可以获得,然后给button注册两个监听器,一个是OnclickListener,另一个是OnLongClickListener,然后我们这里通过匿名内部类来得到实现了这两个接口的对象,实现了其抽象方法。这样我们就可以在方法里面实现我们需要的操作了。

Android 组件系列-----Activity初步的更多相关文章

  1. Android组件系列----Activity组件详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  2. Android 组件系列-----Activity的传值和回传值

    在这篇随笔里将讲解Activity的传值和回传值. 一.通过startActivity来进行Activity的传值 在Android中,如果我们要通过一个Activity来启动另一个Activity, ...

  3. Android 组件系列-----Activity生命周期

    本篇随笔将会深入学习Activity,包括如何定义多个Activity,并设置为默认的Activity.如何从一个Activity跳转到另一个Activity,还有就是详细分析Activity的生命周 ...

  4. Android 组件系列-----Activity保存状态

    本篇随笔将详细的讲解Activity保存状态的概念,也就是saving activity state. 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的 ...

  5. Android组件系列----Activity的生命周期

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  6. 【转】Android 组件系列-----Activity保存状态

    本篇随笔将详细的讲解Activity保存状态的概念,也就是saving activity state. 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的 ...

  7. Android组件系列----当前Activity跳转到另一个Activity的详细过程

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  8. Android组件系列----BroadcastReceiver广播接收器

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  9. 第01讲 Android开发系列---Activity

    一.  Android系统版本及详细信息 最新数据  https://developer.android.com/about/dashboards/ 二.  Android项目初探 1.    使用a ...

随机推荐

  1. python全栈开发day22-常用模块二(hashlib、configparse、logging)

    一.昨日内容回顾 1.钻石继承 #新式类,本身或父类显示继承object #找名字的时候是广度优先顺序 #有mro方法,super方法, # super并不是单纯的找父类,和mro顺序是完全对应的 # ...

  2. Python urllib Request 用法

    转载自:https://blog.csdn.net/ywy0ywy/article/details/52733839 python2.7 httplib, urllib, urllib2, reque ...

  3. C#: 执行批处理文件(*.bat)的方法

    static void Main(string[] args) { Process proc = null; try { proc = new Process(); proc.StartInfo.Fi ...

  4. P1019 单词接龙 字符串回溯

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

  5. python模块——PrettyTable

    python模块——PrettyTable 一. 简介 Python通过prettytable模块将输出内容如表格方式整齐输出,可用来生成美观的ASCII格式的表格,十分实用. python本身并不内 ...

  6. P2031 脑力达人之分割字串

    P2031 脑力达人之分割字串字符串dp,f[i]表示主串到第i个字符,最多能分割成多少子串.f[i]=max(f[i],f[k]+1);k是能匹配到的前一位. #include<iostrea ...

  7. 条件随机场之CRF++源码详解-训练

    上篇的CRF++源码阅读中, 我们看到CRF++如何处理样本以及如何构造特征.本篇文章将继续探讨CRF++的源码,并且本篇文章将是整个系列的重点,会介绍条件随机场中如何构造无向图.前向后向算法.如何计 ...

  8. git根据用户过滤提交记录

    使用SourceTree 使用gitk

  9. 移动端、PC端(前后台)、小程序常用的UI框架

    1.移动端UI库 ①.Vant UI 官方地址:https://youzan.github.io/vant/#/zh-CN/intro github地址:https://github.com/youz ...

  10. pwcrack--一款集合多种md5解密的工具

    项目开源地址:https://github.com/L-codes/pwcrack-framework Ruby2.5+ (tested with Ruby2.5.3 & Ruby 2.6.3 ...