代表控件的对象

来源:互联网 发布:黑马程序员长沙校区 编辑:程序博客网 时间:2024/04/28 04:22

代表控件的对象

获取xml中控件的对象

在Android开发中,布局文件中定义的控件,在程序代码中都有与之对应的控件对象。

activity_main.xml文件如下:

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

在xml文件中定义的控件,在代码中是一个View对象,不同的控件对应View类的子类,TextView对应TextView对象,Button对应Button对象,在代码中我们可以通过findViewById()方法获得该控件的对象,不过其的得到的是View对象,我们通过向下转型获得其对应的View对象。

获得该控件对象后,我们可以在代码中对其属性进行动态控制了,我们修改activity_main.xml文件:

<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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@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="80px"        android:background="#00ff00"        android:text="@string/hello_world"        /></RelativeLayout>

我们在代码中获取其对应的对象:

package com.example.z1178.test;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends ActionBarActivity {    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView textView=(TextView)findViewById(R.id.textView);        textView.setText("My name is Siege");    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

我们获取该对象后,设置其内容,同样可以修改其他的属性。

控件的监听器

我们获取代表控件的对象后,定义一个类,实现监听器接口,然后生成监听器对象,最后绑定监听器对象。

activity_main.xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@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="80px"        android:background="#00ff00"        android:text="@string/hello_world"        />    <Button        android:id="@+id/button"        android:layout_width="500px"        android:layout_height="100px"        android:text="BUTTON"        /></RelativeLayout>

MainActivity.java:

package com.example.z1178.test;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {    private TextView textView;    private Button button;    private int count;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView=(TextView)findViewById(R.id.textView);        textView.setText("My name is Siege");        textView.setBackgroundColor(Color.BLUE);        button=(Button)findViewById(R.id.button);        //生成监听器对象        ButtonListener buttonListener=new ButtonListener();        button.setOnClickListener(buttonListener);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    /**     *     *生成监听器类     */    class ButtonListener implements View.OnClickListener{        @Override        public void onClick(View v) {            count++;            textView.setText(count+"");        }    }}

Android中有很多不同的监听器,但是使用的步骤都是一样的。

0 0