在上篇博文讲解了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,然后我们这里通过匿名内部类来得到实现了这两个接口的对象,实现了其抽象方法。这样我们就可以在方法里面实现我们需要的操作了。

最新文章

  1. ab post 测试 http 和 webservice 接口方法及用例
  2. 加密和ssl机制细节
  3. 机器学习实战 - 读书笔记(05) - Logistic回归
  4. WPF中的WebBrowser
  5. class属性添加多个类
  6. 【四】注入框架RoboGuice使用:(Your First System Service Injection)
  7. iOS 错误 之 http请求
  8. POJ 1308 Is It A Tree? 解题报告
  9. JSP基础概要
  10. 什么是Tensor
  11. sql server中的while循环语句
  12. centos7-软件安装-mysql5.7
  13. Linux备份压缩命令
  14. Spring JDBC处理CLOB类型字段
  15. PAT 列车厢调度&#160;&#160;&#160;(25分)(栈和容器的简单应用)
  16. javascript 千分
  17. free 和 delete 把指针怎么了
  18. wait和waitpid
  19. 关于 double sort 这道题的思考
  20. git 设置代理.

热门文章

  1. python全栈开发知识点补充for else和while else如果不是除正常以外的其他方式退出循环,那么else语句就会被执行。
  2. Codeforces 535D - Tavas and Malekas
  3. BZOJ1266 [AHOI2006]上学路线route Floyd 最小割 SAP
  4. 第六章|网络编程-socket开发
  5. 037 对于HIVE架构的理解
  6. C#多线程编程实战(二):线程同步
  7. 安卓android杀不死进程,保护,双进程守护,驻留,Marsdaemon,保活
  8. CAD画图技巧经验
  9. Windows7的MySQL数据库的安装
  10. Java 中的“implements Runnable” 和“extends Thread”(转)