Android控件以及各布局空间的使用方式_android控件及布局

2020-02-28 其他范文 下载本文

Android控件以及各布局空间的使用方式由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“android控件及布局”。

控件讲解

? ? ? 在 Windows 下搭建 Android 开发环境 Android 项目的目录结构说明 写一个简单的 Hello World 程序

一、在 Windows 下搭建 Android 开发环境

1、安装 JDK(Java Development Kit)http://download.java.net/jdk6/

2、安装 Android SDK http://developer.android.com/sdk

3、安装 Eclipse http://www.daodoc.com/

4、打开 Eclipse,并安装其 Android 插件(ADT)

打开菜单 “Help”-> “Install New Software”,在 “Availabe Software” 中加入地址 http://dl-l.google.com/android/eclipse/,然后安装 ADT(Android Development Tools)

5、新建 Android 项目

“New”-> Android Project,Project Name编译项目的 SDK 版本;Application name包名;Min SDK Version用于放置源程序

2、gen用于放置原始文件,Android 不会对此目录下的文件做任何处理,这是其与 res 目录不同的地方

4、res/drawable用于放置布局用的 xml 文件;res/valuesAndroid 程序的清单文件,相当于配置文件,配置应用程序名称、图标、Activity、Service、Receiver等

三、Hello World 程序

1、res/layout/main.xml 代码

android:orientation=“vertical”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:id=“@+id/layout”

>

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:text=“@string/hello”

/>

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:id=“@+id/txt”

/>

2、res/values/strings.xml 代码

layout 直接调用 values 中的字符串

编程方式调用 values 中的字符串

webabcd_hello

3、res/drawable 目录下放置一个名为 icon.png 的图片文件

4、AndroidManifest.xml 代码

package=“com.webabcd.hello”

android:versionCode=“1”

android:versionName=“1.0”>

android:label=“@string/app_name”>

5、Main.java 代码

package com.webabcd.hello;

import android.app.Activity;import android.os.Bundle;import android.widget.LinearLayout;import android.widget.TextView;

public cla Main extends Activity {

/** Called when the activity is first created.*/

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

// 将指定的布局文件作为 Activity 所显示的内容

setContentView(R.layout.main);

// 动态地在指定的容器控件上添加新的控件

TextView txt = new TextView(this);

txt.setText(“动态添加控件”);

// setContentView(txt);

((LinearLayout)this.findViewById(R.id.layout)).addView(txt);

// 引用资源文件内的内容作为输出内容

TextView txt1 =(TextView)this.findViewById(R.id.txt);

txt1.setText(this.getString(R.string.hello2));

} }

四、系出名门Android(2)宽。fill_parent: 宽度跟着父元素走;wrap_content: 宽度跟着本身的内容走;直接指定一个 px 值来设置宽

layout_height线形布局。

orientation内容的排列形式。常用的有 top, bottom, left, right, center 等,详见文档-->

android:orientation=“vertical” android:gravity=“right”

android:layout_width=“fill_parent” android:layout_height=“fill_parent”>

FrameLayout表格式布局。

TableRow设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开

stretchColumns设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,”隔开

-->

android:layout_width=“fill_parent” android:layout_height=“wrap_content”

android:collapseColumns=“1”>

android:layout_height=“wrap_content”>

android:layout_weight=“1” android:layout_height=“wrap_content”

android:text=“行1列1” />

android:layout_weight=“1” android:layout_height=“wrap_content”

android:text=“行1列2” />

android:layout_weight=“1” android:layout_height=“wrap_content”

android:text=“行1列3” />

android:layout_height=“wrap_content”>

android:layout_height=“wrap_content” android:text=“行2列1” />

AbsoluteLayoutx 坐标。以左上角为顶点

layout_y相对定位布局。

layout_centerInParent设置当前元素相对于其容器的左侧边缘的距离

layout_below当前元素与指定的元素右对齐

-->

android:layout_width=“fill_parent” android:layout_height=“fill_parent”>

android:layout_height=“wrap_content” android:text=“centerInParent=true”

android:layout_centerInParent=“true” />

android:layout_height=“wrap_content” android:text=“marginLeft=20px”

android:layout_marginLeft=“20px” />

android:layout_height=“wrap_content” android:text=“xxx”

android:layout_below=“@id/abc” android:layout_alignRight=“@id/abc” />

res/values/strings.xml

Hello Layout

webabcd_layout

Main.java 代码

package com.webabcd.layout;

import android.app.Activity;import android.os.Bundle;

public cla Main extends Activity {

/** Called when the activity is first created.*/

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

} }

2、上下文菜单,选项菜单,子菜单

res/layout/main.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

android:layout_height=“wrap_content” android:text=“@string/hello_contextMenu” />

android:layout_height=“wrap_content” android:text=“@string/hello_subMenu” />

res/values/strings.xml 代码

Hello Context Menu

Hello Context Sub Menu

webabcd_menu

Main.java 代码

package com.webabcd.menu;

import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuItem;import android.view.SubMenu;import android.view.View;import android.view.ContextMenu.ContextMenuInfo;import android.widget.TextView;import android.widget.Toast;

// 演示两种菜单的实现方式:上下文菜单(通过在某元素上长按,来呼出菜单)和选项菜单(通过按手机上的菜单按钮,来呼出菜单)public cla Main extends Activity {

/** Called when the activity is first created.*/

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 为 R.id.txt1 注册一个上下文菜单(在此 TextView 上长按,则会呼出上下文菜单)

// 具体呼出的菜单内容需要重写 onCreateContextMenu 来创建

TextView txt1 =(TextView)this.findViewById(R.id.txt1);

this.registerForContextMenu(txt1);

// 为 R.id.txt2 注册一个上下文菜单

TextView txt2 =(TextView)this.findViewById(R.id.txt2);

this.registerForContextMenu(txt2);

}

// 重写 onCreateContextMenu 用以创建上下文菜单

// 重写 onContextItemSelected 用以响应上下文菜单

@Override

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo){

super.onCreateContextMenu(menu, v, menuInfo);

// 创建 R.id.txt1 的上下文菜单

if(v ==(TextView)this.findViewById(R.id.txt1)){

// ContextMenu.setIcon()设置菜单的标题

menu.setHeaderIcon(R.drawable.icon01);

menu.setHeaderTitle(“我是菜单”);

// 用 ContextMenu.add()来增加菜单项,返回值为 MenuItem

// 第一个参数:组ID

// 第二个参数:菜单项ID

// 第三个参数:顺序号

// 第四个参数:菜单项上显示的内容

menu.add(1, 0, 0, “菜单1”);

// MenuItem用来添加子菜单。子菜单其实就是一个特殊的菜单

SubMenu sub = menu.addSubMenu(“父菜单1”);

sub.setIcon(R.drawable.icon01);

sub.add(0, 0, 0, “菜单1”);

sub.add(0, 1, 1, “菜单2”);

sub.setGroupCheckable(1, true, true);

SubMenu sub2 = menu.addSubMenu(“父菜单2”);

sub2.setIcon(R.drawable.icon01);

sub2.add(1, 0, 0, “菜单3”);

sub2.add(1, 1, 1, “菜单4”);

sub2.setGroupCheckable(1, true, false);

}

}

// 重写 onCreateOptionsMenu 用以创建选项菜单

@Override

public boolean onCreateOptionsMenu(Menu menu){

MenuItem menuItem = menu.add(0, 0, 0, “菜单***111111”);

// MenuItem.setIcon()菜单的简标题,如果指定了简标题的话,菜单项上的标题将会以此简标题为准

// MenuItem.setAlphabeticShortcut()用于表现功能

服务(Service)用于发送广播

广播接收器(BroadcastReceiver)用于连接以上各个组件,并在其间传递消息

1、演示 Activity 的基本用法,一个 Activity 启动另一个 Activity,启动另一个 Activity 时为其传递参数,被启动的 Activity 返回参数给启动者的 Activity Main.java 代码

package com.webabcd.activity;

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;

public cla Main extends Activity {

TextView txt;

/** Called when the activity is first created.*/

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

this.setContentView(R.layout.main);

txt =(TextView)this.findViewById(R.id.txt);

txt.setText(“Activity 1”);

Button btn =(Button)this.findViewById(R.id.btn);

btn.setText(“启动另一个Activity”);

btn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v){

// 实例化 Intent,指定需要启动的 Activity

Intent intent = new Intent();

intent.setCla(Main.this, MyActivity.cla);

// 实例化 Bundle,设置需要传递的参数

Bundle bundle = new Bundle();

bundle.putString(“name”, “webabcd”);

bundle.putDouble(“salary”, 100.13);

// 将需要传递的参数赋值给 Intent 对象

intent.putExtras(bundle);

// startActivity(intent);// 启动指定的 Intent(不等待返回结果)

// Main.this.finish();

// 启动指定的 Intent,并等待返回结果

// 其中第二个参数如果大于等于零,则返回结果时会回调 onActivityResult()方法

startActivityForResult(intent, 0);

}

});

Log.d(“MyDebug”, “onCreate”);

}

// 被启动的 Activity 返回结果时的回调函数

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data){

if(resultCode == Activity.RESULT_OK){

Bundle bundle = data.getExtras();

String name = bundle.getString(“name”);

double salary = bundle.getDouble(“salary”);

txt.setText(“Activity 1” + “nString.valueOf(salary));

名字:” + name + “n

薪水:” +

}

}

@Override

protected void onStart(){

// TODO Auto-generated method stub

super.onStart();

Log.d(“MyDebug”, “onStart”);

}

@Override

protected void onStop(){

// TODO Auto-generated method stub

super.onStop();

Log.d(“MyDebug”, “onStop”);

}

@Override

protected void onRestart(){

// TODO Auto-generated method stub

super.onRestart();

Log.d(“MyDebug”, “onRestart”);

}

@Override

protected void onPause(){

// TODO Auto-generated method stub

super.onPause();

Log.d(“MyDebug”, “onPause”);

}

@Override

protected void onResume(){

// TODO Auto-generated method stub

super.onResume();

Log.d(“MyDebug”, “onResume”);

}

@Override

protected void onDestroy(){

// TODO Auto-generated method stub

super.onDestroy();

Log.d(“MyDebug”, “onDestroy”);

} }

MyActivity.java 代码

package com.webabcd.activity;

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;

// 被另一个 Activity 所启动的 Activity public cla MyActivity extends Activity {

Intent intent;

/** Called when the activity is first created.*/

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

this.setContentView(R.layout.main2);

// 获取启动者传递过来的参数

intent = this.getIntent();

Bundle bundle = intent.getExtras();

String name = bundle.getString(“name”);

double salary = bundle.getDouble(“salary”);

TextView txt =(TextView)this.findViewById(R.id.txt);

txt.setText(“Activity 2” + “n名字:” + name + “n薪水:” + String.valueOf(salary));

Button btn =(Button)this.findViewById(R.id.btn);

btn.setText(“返回前一个Activity”);

btn.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v){

// 返回参数给启动者

MyActivity.this.setResult(Activity.RESULT_OK, intent);

MyActivity.this.finish();

}

});

} }

AndroidManifest.xml 代码

package=“com.webabcd.activity” android:versionCode=“1”

android:versionName=“1.0”>

如果有需要用到的 Activity,则都要在这里做相应的配置

-->

2、Service, Broadcast, BroadcastReceiver 的演示

Main.java 代码

package com.webabcd.service;

import android.app.Activity;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;/* * startService()和 bindService()的区别

* startService()使当前上下文对象(本例中就是 Activity)通过一个 ServiceConnection 对象邦定到指定的 Service。这样,如果上下文对象销毁了的话,那么其对应的 Service 也会被销毁

*/ public cla Main extends Activity implements OnClickListener {

private TextView txtMsg;

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setTitle(“android 之 service”);

this.findViewById(R.id.btnStart).setOnClickListener(this);

this.findViewById(R.id.btnStop).setOnClickListener(this);

this.findViewById(R.id.btnBind).setOnClickListener(this);

this.findViewById(R.id.btnUnbind).setOnClickListener(this);

txtMsg =(TextView)this.findViewById(R.id.txtMsg);

// 实例化自定义的

BroadcastReceiver

receiver = new UpdateReceiver();

IntentFilter filter = new IntentFilter();

// 为 BroadcastReceiver 指定 action,使之用于接收同 action 的广播

filter.addAction(“com.webabcd.service.msg”);

// 以编程方式注册

BroadcastReceiver。配置方式注册 BroadcastReceiver 的例子见 AndroidManifest.xml 文件

// 一般在 OnStart 时注册,在 OnStop 时取消注册

this.registerReceiver(receiver, filter);

// this.unregisterReceiver(receiver);

}

@Override

public void onClick(View v){

Intent intent = new Intent(Main.this, MyService.cla);

switch(v.getId()){

case R.id.btnStart:

this.startService(intent);

break;

case R.id.btnStop:

this.stopService(intent);

break;

case R.id.btnBind:

this.bindService(intent, conn, Context.BIND_AUTO_CREATE);

break;

case R.id.btnUnbind:

this.unbindService(conn);

break;

}

}

// bindService()所需的 ServiceConnection 对象

private ServiceConnection conn = new ServiceConnection(){

@Override

public void onServiceConnected(ComponentName claName, IBinder service){

}

@Override

public void onServiceDisconnected(ComponentName claName){

}

};

private String msg=“”;

private UpdateReceiver receiver;

// 实现一个 BroadcastReceiver,用于接收指定的 Broadcast

public cla UpdateReceiver extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent){

msg = intent.getStringExtra(“msg”);

txtMsg.append(msg + “n”);

}

} }

MyService.java 代码

package com.webabcd.service;

import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;

// 演示 Service 的生命周期。具体信息运行程序后在 LogCat 中查看 public cla MyService extends Service {

@Override

public IBinder onBind(Intent intent){

Log.d(“MyDebug”, “onBind”);

sendMsg(“onBind”);

// TODO Auto-generated method stub

return null;

}

@Override

public void onCreate(){

// TODO Auto-generated method stub

super.onCreate();

Log.d(“MyDebug”, “onCreate”);

sendMsg(“onCreate”);

}

@Override

public void onDestroy(){

// TODO Auto-generated method stub

super.onDestroy();

Log.d(“MyDebug”, “onDestroy”);

sendMsg(“onDestroy”);

}

@Override

public void onRebind(Intent intent){

// TODO Auto-generated method stub

super.onRebind(intent);

Log.d(“MyDebug”, “onRebind”);

sendMsg(“onRebind”);

}

@Override

public void onStart(Intent intent, int startId){

super.onStart(intent, startId);

Log.d(“MyDebug”, “onStart”);

sendMsg(“onStart”);

}

@Override

public boolean onUnbind(Intent intent){

Log.d(“MyDebug”, “onUnbind”);

sendMsg(“onUnbind”);

// TODO Auto-generated method stub

return super.onUnbind(intent);

}

// 发送广播信息

private void sendMsg(String msg){

// 指定广播目标的 action(注:指定了此 action 的 receiver 会接收此广播)

Intent intent = new Intent(“com.webabcd.service.msg”);

// 需要传递的参数

intent.putExtra(“msg”, msg);

// 发送广播

this.sendBroadcast(intent);

} }

MyBootReceiver.java 代码

package com.webabcd.service;

import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public cla MyBootReceiver extends BroadcastReceiver {

// 用于接收满足条件的 Broadcast(相应的 Broadcast 的注册信息详见 AndroidManifest.xml,当系统启动完毕后会调用这个广播接收器)

@Override

public void onReceive(Context arg0, Intent arg1){

Log.d(“MyDebug”, “onReceive”);

// 启动服务

Intent service = new Intent(arg0, MyService.cla);

arg0.startService(service);

} }

AndroidManifest.xml 代码

package=“com.webabcd.service” android:versionCode=“1”

android:versionName=“1.0”>

如果有需要用到的 service,则都要在这里做相应的配置

-->

注册一个 BroadcastReceiver

其 intent-filter 为 android.intent.action.BOOT_COMPLETED(用于接收系统启动完毕的 Broadcast)

-->

接受系统启动完毕的 Broadcast 的权限

-->

android:name=“android.permiion.RECEIVE_BOOT_COMPLETED” Android控件

在 Android 中使用各种控件(View)? ? ? ? ? ? ? ? TextView按钮控件

ImageButton图片显示控件

CheckBox单选框控件

AnalogClock电子表控件

1、TextView(文本显示控件)的 Demo textview.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

TextView按钮控件

-->

android:layout_width=“wrap_content” android:layout_height=“wrap_content”>

_Button.java 代码

package com.webabcd.view;

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;

public cla _Button extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState){

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.button);

setTitle(“Button”);

Button btn =(Button)this.findViewById(R.id.button);

btn.setText(“click me”);

// setOnClickListener()图片按钮控件

-->

android:layout_width=“wrap_content” android:layout_height=“wrap_content”>

_ImageButton.java 代码

package com.webabcd.view;

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.TextView;

public cla _ImageButton extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState){

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.imagebutton);

setTitle(“ImageButton”);

ImageButton imgButton =(ImageButton)this.findViewById(R.id.imageButton);

// 设置图片按钮的背景

imgButton.setBackgroundResource(R.drawable.icon01);

// setOnClickListener()图片显示控件

-->

android:layout_height=“wrap_content”>

_ImageView.java 代码

package com.webabcd.view;

import android.app.Activity;import android.os.Bundle;import android.widget.ImageView;

public cla _ImageView extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState){

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.imageview);

setTitle(“ImageView”);

ImageView imgView =(ImageView)this.findViewById(R.id.imageView);

// 指定需要显示的图片

imgView.setBackgroundResource(R.drawable.icon01);

} }

5、CheckBox(复选框控件)的 Demo checkbox.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

android:layout_height=“wrap_content” android:id=“@+id/textView” />

CheckBox响应复选框的选中状态改变事件

chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

@Override

public isChecked){

TextView txt =(TextView)_CheckBox.this.findViewById(R.id.textView);

txt.setText(“CheckBox01 的选中状态:” + String.valueOf(isChecked));

}

});

} } void

onCheckedChanged(CompoundButton

buttonView,boolean6、RadioButton(单选框控件)的 Demo radiobutton.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

android:layout_height=“wrap_content” android:id=“@+id/textView” />

RadioButton对其内的单选框控件做分组

checkedButton响应单选框组内的选中项发生变化时的事件

group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

@Override

public void onCheckedChanged(RadioGroup group, int checkedId){

TextView txt =(TextView)_RadioButton.this.findViewById(R.id.textView);

txt.setText(((RadioButton)findViewById(checkedId)).getText()+ “ 被选中”);

}

});

} }

7、AnalogClock(钟表(带表盘的那种)控件)的 Demo analogclock.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

AnalogClock电子表控件

-->

android:layout_width=“wrap_content” android:layout_height=“wrap_content”>

_DigitalClock.java 代码

package com.webabcd.view;

import android.app.Activity;import android.os.Bundle;

public cla _DigitalClock extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState){

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.digitalclcok);

setTitle(“DigitalClcok”);

} }

? ? ? ? ? ? ? ? DatePicker时间选择控件 ToggleButton可编辑文本控件 ProgreBar可拖动的进度条控件

AutoCompleteTextView支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的分隔符分开)

9、DatePicker(日期选择控件)的 Demo datepicker.xml

代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

DatePicker时间选择控件

-->

android:layout_width=“wrap_content” android:layout_height=“wrap_content”>

_TimePicker.java 代码

package com.webabcd.view;

import android.app.Activity;import android.os.Bundle;

public cla _TimePicker extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState){

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.timepicker);

// 具体的应用可参见对话框中的示例

setTitle(“TimePicker”);

} }

11、ToggleButton(双状态按钮控件)的 Demo togglebutton.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

android:layout_height=“wrap_content” android:id=“@+id/textView” />

ToggleButton当按钮状态为 true 时所显示的文本

textOff响应按钮的鼠标单击事件

btn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v){

TextView txt =(TextView)_ToggleButton.this.findViewById(R.id.textView);

// ToggleButton.isChecked()可编辑文本控件

-->

android:layout_height=“wrap_content”>

_EditText.java 代码

package com.webabcd.view;

import android.app.Activity;import android.os.Bundle;import android.widget.EditText;

public cla _EditText extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState){

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

this.setContentView(R.layout.edittext);

setTitle(“EditText”);

EditText txt =(EditText)this.findViewById(R.id.editText);

txt.setText(“我可编辑”);

} }

13、ProgreBar(进度条控件)的 Demo progrebar.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

ProgreBar进度条的样式,本例使用内置样式

max第一进度位置

secondaryProgre可拖动的进度条控件

max第一进度位置

secondaryProgre响应拖动进度条事件

mSeekBar.setOnSeekBarChangeListener(this);

mProgreText =(TextView)findViewById(R.id.progre);

mTrackingText =(TextView)findViewById(R.id.tracking);

}

// 拖动进度条后,进度发生改变时的回调事件

public void onProgreChanged(SeekBar seekBar, int progre,boolean fromTouch){

mProgreText.setText(progre + “%”);

}

// 拖动进度条前开始跟踪触摸

public void onStartTrackingTouch(SeekBar seekBar){

mTrackingText.setText(“开始跟踪触摸”);

}

// 拖动进度条后停止跟踪触摸

public void onStopTrackingTouch(SeekBar seekBar){

mTrackingText.setText(“停止跟踪触摸”);

} } 例图:

15、AutoCompleteTextView(支持自动完成功能的可编辑文本控件)的 Demo autocompletetextview.xml 代码

android:orientation=“vertical” android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

AutoCompleteTextView-支持自动完成功能的可编辑文本控件

-->

android:layout_width=“fill_parent” android:layout_height=“wrap_content” />

_AutoCompleteTextView.java 代码

package com.webabcd.view;

import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;

public cla _AutoCompleteTextView extends Activity {

《Android控件以及各布局空间的使用方式.docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
Android控件以及各布局空间的使用方式
点击下载文档
相关专题 android控件及布局 控件 布局 方式 android控件及布局 控件 布局 方式
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文