Android 组件系列-----Activity初步

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

原文地址:https://www.cnblogs.com/xiaoluo501395377/p/3389434.html