代表控件的对象
来源:互联网 发布:黑马程序员长沙校区 编辑:程序博客网 时间: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
- 代表控件的对象
- super在对象的内部使用,代表父类对象
- 数字代表的意思
- 类名后的*代表什么
- /proc/* 代表的意义
- Activity的代表ActivityRecord
- 美好的代表
- 代表颜色的英文单词
- 常对象c++函数后面的CONST代表if的作用域
- css中的*代表对所有控件起作用
- 每个Error对象都代表特定的提供者错误而不是ADO错误
- XMLHttpRequest对象发送请求后,请求的状态有几个值,每个值代表什么意思?
- 「源」toSource() //代表对象的源代码 20140828 ①文本处理
- 对象/类,函数/方法 的区分(仅代表个人观点,如有偏差欢迎指正)
- AngularJs在ng-click函数中如何获取代表当前元素的DOM对象
- AngularJs在ng-click函数中如何获取代表当前元素的DOM对象
- AngularJs1在ng-click函数中如何获取代表当前元素的DOM对象
- 代表
- Delete Node in a Linked List
- 杭电 hdu 1102 Constructing Roads (最小生成树)
- Linux 2.6.35内核配置 armv7 s5pv210 模块加载错误
- hdu2181 dfs
- Android应用的耗电量统计
- 代表控件的对象
- MySQL 忘记密码解决方案
- Java IO练习
- [LeetCode][Java] Merge Sorted Array
- 水题
- 操作符--自动递增和递减(前缀式和后缀式的区别、i++和++i区别)
- 也谈double dispatch(双分派)::Visitor 模式(转)
- Lowest Common Ancestor of a Binary Search Tree
- java,json-lib-2.2.2-jdk15关键字bug