当前位置: 编程技术>移动开发
本页文章导读:
▪一个相仿Tabs的控件SegmentControl 一个类似Tabs的控件SegmentControl
package com.ql.view;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import an.........
▪ intent步骤使用总结 intent方法使用总结
//show webapp:
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
//show maps:
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Ac.........
▪ ToggleButton的施用有感 ToggleButton的使用有感
今天用到ToggleButton开关按钮,由于给他设置错了监听事件,效果不是所想 原来用的多的还是监听状态的改变。写一个例子,显示效果才明白。package com.rotunda.test;
impor.........
[1]一个相仿Tabs的控件SegmentControl
来源: 互联网 发布时间: 2014-02-18
一个类似Tabs的控件SegmentControl
用法:
text6.xml
妙用TabHost
http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html
package com.ql.view;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
public class SegmentControl extends LinearLayout {
private Map<Integer,IButton> indexButtonMap = new HashMap<Integer, IButton>();
private Map<IButton,Integer> buttonIndexMap = new HashMap<IButton, Integer>();
private int selectedIndex;
public static final int TAB = 1;
public static final int SEGMENT = 2;
private int currentStyle = SEGMENT;
private int maxButtonSize;
private int marginsLeft = 1;
private LayoutParams layoutMarginsParams;
private boolean onlyIsPressed;
private OnSegmentChangedListener onSegmentChangedListener;
public SegmentControl(Context context, AttributeSet attrs) {
super(context,attrs);
this.setOrientation(HORIZONTAL);
layoutMarginsParams = new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutMarginsParams.setMargins(marginsLeft, 0, 0, 0);
}
public SegmentControl(Context context,int style) {
super(context,null);
this.setOrientation(HORIZONTAL);
currentStyle = style;
layoutMarginsParams = new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutMarginsParams.setMargins(marginsLeft, 0, 0, 0);
}
public void setStyle(int style) {
currentStyle = style;
}
public void setWidth(int width, int height, int num) {
int itemWidth = width/num;
layoutMarginsParams.width = itemWidth;
layoutMarginsParams.height = height;
}
public int getButtonCount(){
return maxButtonSize;
}
public IButton getButton(int index){
return indexButtonMap.get(index);
}
public void setSelectedIndex(int index){
if(index <= maxButtonSize){
selectedIndex = index;
selectButton(index);
}
}
public int getSelectedIndex(){
return selectedIndex;
}
/**
* 废弃
* 请使用setOnSegmentChangedListener代替
* @param index
* @param l
*/
@Deprecated
public void bindOnChooseListener(int index, IButton.OnChooseListener l){
indexButtonMap.get(index).setOnChooseListener(l);
}
public void clearButton() {
this.removeAllViews();
maxButtonSize = 0;
}
public IButton newButton(int drawableId, int id){
IButton button = new IButton(getContext(), id, IButton.PICTURE);
button.setLayoutParams(layoutMarginsParams);
button.setBackgroundResource(drawableId);
postNewButton(button);
return button;
}
private void postNewButton(IButton button){
this.addView(button);
addButtonToMap(button, maxButtonSize);
maxButtonSize++;
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
selectedIndex = buttonIndexMap.get(v);
selectButton(selectedIndex);
}
return false;
}
});
}
public IButton newButton(String text, int id){
IButton button = null;
if(currentStyle == TAB){
button = new IButton(getContext(), id, IButton.TAB);
}else if(currentStyle == SEGMENT){
if(maxButtonSize == 0){
button = new IButton(getContext(), id);
}else{
button = new IButton(getContext(), id, IButton.SEGMENT_CENTER);
}
//只有2个按�?
if(maxButtonSize == 1){
getButton(0).changeButtonStyle(IButton.SEGMENT_LEFT);
button.changeButtonStyle(IButton.SEGMENT_RIGHT);
}
//超过2�?
if(maxButtonSize > 1){
getButton(0).changeButtonStyle(IButton.SEGMENT_LEFT);
getButton(maxButtonSize - 1).changeButtonStyle(IButton.SEGMENT_CENTER);
button.changeButtonStyle(IButton.SEGMENT_RIGHT);
}
}
//layoutMarginsParams = new LayoutParams(45, 35);
button.setLayoutParams(layoutMarginsParams);
//button背景色可以在这里设置
button.setPressedColor(Color.rgb(16, 38, 55), Color.rgb(16, 38, 55));
button.setTextSize(16);
button.setText(text);
postNewButton(button);
return button;
}
private void addButtonToMap(IButton button, int index){
this.indexButtonMap.put(maxButtonSize, button);
this.buttonIndexMap.put(button, maxButtonSize);
}
private void selectButton(int index){
//1
if(maxButtonSize == 1){
IButton button = indexButtonMap.get(0);
button.onDefaultUp();
if(!onlyIsPressed){
button.onDown();
if(button.hasPressedDrawable()){
button.setPressedDrawable();
}
if(onSegmentChangedListener != null){
onSegmentChangedListener.onSegmentChanged(button.getCmdId());
}
onlyIsPressed = true;
}else{
if(button.hasDefaultDrawable()){
button.setDefaultDrawable();
}
button.onUp();
onlyIsPressed = false;
}
//more
}else{
for (int i = 0; i < maxButtonSize; i++) {
IButton button = indexButtonMap.get(i);
if(i == index){
if(button.isNormal()){
button.onDown();
if(button.hasPressedDrawable()){
button.setPressedDrawable();
}
if(onSegmentChangedListener != null){
onSegmentChangedListener.onSegmentChanged(button.getCmdId());
}
}
}else{
if(button.hasDefaultDrawable()){
button.setDefaultDrawable();
}
button.onDefaultUp();
}
}
}
}
public interface OnSegmentChangedListener{
public void onSegmentChanged(int index);
}
public void setOnSegmentChangedListener(OnSegmentChangedListener l){
this.onSegmentChangedListener = l;
}
}
package com.ql.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
/**
*/
public class IButton extends Button implements OnTouchListener {
private int buttonID;
private ShapeDrawable mDrawable;
private boolean isPressed = false;
private int radian;
float[] DEFAULT_OUTRADII;
float[] TAB_OUTRADII;
float[] LEFT_OUTRADII;
float[] RIGHT_OUTRADII;
float[] CENTER_OUTRADII;
private static int DEFAULT_RADIAN = 8;
//#B7B7B7
private int DEFAULT_START_COLOR = -4737097;
//8F8F8F
private int DEFAULT_END_COLOR = -7368817;
//#9F9F9F
private int PRESSED_START_COLOR = -6316129;
//#767676
private int PRESSED_END_COLOR = -9013642;
public static int TAB = 1;
public static int SEGMENT_LEFT = 2;
public static int SEGMENT_CENTER = 3;
public static int SEGMENT_RIGHT = 4;
public static int DEFAULT = 0;
public static int PICTURE = 5;
private int style;
private OnChooseListener mOnChooseListener;
/**
* 默认图片
*/
private int defaultDrawableId;
/**
* 按下图片
*/
private int pressedDrawableId;
public boolean hasDefaultDrawable(){
if(defaultDrawableId != 0){
return true;
}else{
return false;
}
}
public boolean hasPressedDrawable(){
if(pressedDrawableId != 0){
return true;
}else{
return false;
}
}
public void setDefaultDrawableId(int defaultDrawableId){
this.defaultDrawableId = defaultDrawableId;
}
public void setDefaultDrawable(int defaultDrawableId){
setDefaultDrawableId(defaultDrawableId);
setDefaultDrawable();
}
public void setDefaultDrawable(){
setBackgroundResource(defaultDrawableId);
}
public void setPressedDrawable(int pressedDrawableId){
setPressedDrawableId(pressedDrawableId);
setPressedDrawable();
}
public void setPressedDrawable(){
setBackgroundResource(pressedDrawableId);
}
public void setPressedDrawableId(int pressedDrawableId){
this.pressedDrawableId = pressedDrawableId;
}
public void setOnChooseListener(IButton.OnChooseListener l){
this.mOnChooseListener = l;
}
public boolean isNormal(){
return !isPressed;
}
public boolean isPressed(){
return isPressed;
}
public void setRadian(int radian){
this.radian = radian;
initRadian();
changeButtonStyle(style);
}
private void initRadian(){
DEFAULT_OUTRADII = new float[] { radian, radian, radian, radian, radian, radian, radian, radian };
TAB_OUTRADII = new float[] { radian, radian, radian, radian, 0, 0, 0, 0 };
LEFT_OUTRADII = new float[] { radian, radian, 0, 0, 0, 0, radian, radian };
RIGHT_OUTRADII = new float[] { 0, 0, radian, radian, radian, radian, 0, 0 };
CENTER_OUTRADII = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };
}
/**
*
* @param startColor
* @param endColor
*/
public IButton setNormalColor(int startColor, int endColor){
this.DEFAULT_START_COLOR = startColor;
this.DEFAULT_END_COLOR = endColor;
invalidate();
return this;
}
/**
*
* @param startColor
* @param endColor
*/
public IButton setPressedColor(int startColor, int endColor){
this.PRESSED_START_COLOR = startColor;
this.PRESSED_END_COLOR = endColor;
invalidate();
return this;
}
private Shader getNormalColor(int width, int height){
return new LinearGradient(width/2,0,width/2,height,DEFAULT_START_COLOR,DEFAULT_END_COLOR,Shader.TileMode.MIRROR);
}
private Shader getPressedColor(int width, int height){
return new LinearGradient(width/2,0,width/2,height,PRESSED_START_COLOR, PRESSED_END_COLOR,Shader.TileMode.MIRROR);
}
public IButton(Context context, int id, int style) {
super(context,null);
this.buttonID = id;
init(style);
}
public IButton(Context context, int id){
super(context,null);
this.buttonID = id;
init(DEFAULT);
}
private void init(int style){
radian = DEFAULT_RADIAN;
initRadian();
if(PICTURE != style){
if(mDrawable == null){
mDrawable = getShapeDrawable(style);
}
this.getBackground().setAlpha(0);
this.setTextColor(Color.WHITE);
}
this.setOnTouchListener(this);
}
public IButton(Context context, int id, AttributeSet attrs) {
super(context,attrs);
this.buttonID = id;
init(DEFAULT);
}
public IButton(Context context, int id, ShapeDrawable mDrawable){
super(context);
this.buttonID = id;
this.mDrawable = mDrawable;
}
public int getCmdId() {
return buttonID;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(mDrawable != null){
mDrawable.setBounds(0, 0, this.getWidth(), this.getHeight());
if(!isPressed){
mDrawable.getPaint().setShader(getNormalColor(this.getWidth(), this.getHeight()));
}else{
mDrawable.getPaint().setShader(getPressedColor(this.getWidth(), this.getHeight()));
}
//mDrawable.getPaint().setColor(Color.BLUE);
//mDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
mDrawable.draw(canvas);
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(getTextSize());
paint.setColor(Color.WHITE);
FontMetrics fm = paint.getFontMetrics();
int y = getTop() + (int)(getHeight() - fm.ascent)/2;
canvas.drawText((String)getText(), getWidth()/2, y, paint);
}
public void onDown() {
onDefaultDown();
if(mOnChooseListener != null){
mOnChooseListener.onDown();
}
}
public void onUp() {
onDefaultUp();
if(mOnChooseListener != null){
mOnChooseListener.onUp();
}
}
public void onDefaultUp(){
isPressed = false;
invalidate();
}
public void onDefaultDown(){
isPressed = true;
invalidate();
}
public void changeButtonStyle(int style){
getShapeDrawable(style);
invalidate();
}
private ShapeDrawable getShapeDrawable(int style){
this.style = style;
if(style == TAB){
mDrawable = new ShapeDrawable(new RoundRectShape(TAB_OUTRADII, null,
null));
}else if(style == SEGMENT_LEFT){
mDrawable = new ShapeDrawable(new RoundRectShape(LEFT_OUTRADII, null,
null));
}else if(style == SEGMENT_CENTER){
mDrawable = new ShapeDrawable(new RoundRectShape(CENTER_OUTRADII, null,
null));
}else if(style == SEGMENT_RIGHT){
mDrawable = new ShapeDrawable(new RoundRectShape(RIGHT_OUTRADII, null,
null));
}else{
mDrawable = new ShapeDrawable(new RoundRectShape(DEFAULT_OUTRADII, null,
null));
}
return mDrawable;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!isPressed) {
if(hasPressedDrawable()){
setBackgroundResource(pressedDrawableId);
}
// 更改为按下时的背景图
onDown();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (isPressed) {
if(hasDefaultDrawable()){
setBackgroundResource(defaultDrawableId);
}
// 改为抬起时的图片
onUp();
}
}
// TODO Auto-generated method stub
return false;
}
public interface OnChooseListener{
public void onDown();
public void onUp();
}
}
用法:
package com.ql.activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import com.ql.view.SegmentControl;
import com.ql.view.SegmentControl.OnSegmentChangedListener;
public class Test_5_Activity extends Activity{
SegmentControl segControl;
private ViewFlipper mFlipper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test6);
mFlipper = (ViewFlipper)findViewById(R.id.flipper);
//测试界面,实际开发中是从layout中读取的,下同。
TextView tv=new TextView(this);
tv.setText("index=0");
mFlipper.addView(tv);
// tv.requestFocus();
segControl = (SegmentControl)findViewById(R.id.segcontrol);
segControl.setStyle(SegmentControl.TAB);//试试SEGMENT
segControl.newButton("标题1", 0);
segControl.newButton("标题2", 1);
segControl.newButton("标题3", 2);
segControl.newButton("标题4", 3);
//还可试试segControl.newButton(int drawableId, int id);
segControl.setSelectedIndex(0);
int width = this.px2dip(this, 80*segControl.getButtonCount());
int height = this.px2dip(this, 38);
segControl.setWidth(width, height, segControl.getButtonCount());
segControl.setOnSegmentChangedListener(new OnSegmentChangedListener() {
@Override
public void onSegmentChanged(int index) {
//
onChangeView(index);
}
});
}
private int mIndex=0;
private void changeViewAnimation(int index, View view) {
if(index == mIndex)
return;
mFlipper.addView(view);
Animation inAnim = null;
Animation outAnim = null;
if(index > mIndex) {
inAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
outAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
} else {
inAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
outAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
}
inAnim.setDuration(1000);
outAnim.setDuration(1000);
mFlipper.setInAnimation(inAnim);
mFlipper.setOutAnimation(outAnim);
mFlipper.showNext();
//mFlipper.startFlipping();
mFlipper.removeViewAt(0);
mIndex = index;
}
private void onChangeView(int index)
{
//测试界面,实际开发中是从layout中读取的,下同。
TextView tv=new TextView(this);
tv.setText("index="+index);
switch(index){
case 0:
Toast.makeText(this, "VIEW_TLINE", Toast.LENGTH_SHORT).show();
changeViewAnimation(index, tv);
break;
case 1:
Toast.makeText(this, "VIEW_KLINE", Toast.LENGTH_SHORT).show();
changeViewAnimation(index, tv);
break;
case 2:
Toast.makeText(this, "VIEW_DETAIL", Toast.LENGTH_SHORT).show();
changeViewAnimation(index, tv);
break;
case 3:
Toast.makeText(this, "VIEW_F10", Toast.LENGTH_SHORT).show();
changeViewAnimation(index, tv);
break;
case 4:
Toast.makeText(this, "VIEW_RADAR", Toast.LENGTH_SHORT).show();
changeViewAnimation(index, tv);
break;
}
}
//dip/px像素单位转换
public static int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue / scale + 0.5f);
}
public static int px2dip(Context context, float pxValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(pxValue * scale + 0.5f);
}
}
text6.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.ql.view.SegmentControl android:id="@+id/segcontrol" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> <!-- android:gravity="right" --> <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" /> </LinearLayout>
妙用TabHost
http://www.cnblogs.com/over140/archive/2011/03/02/1968042.html
[2] intent步骤使用总结
来源: 互联网 发布时间: 2014-02-18
intent方法使用总结
//show webapp:
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
//show maps:
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
//show ways
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
//call dial program
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);
//don't forget add this config:<uses-permission id="android.permission.CALL_PHONE" />
//send sms/mms
//call sender program
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
//send sms
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
//send mms
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
//send email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"me@abc.com"};
String[] ccs={"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//add extra
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//play media
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//Uninstall
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
//uninstall apk
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
//install apk
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
//play audio
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
//send extra
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//search
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
//show program detail page
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
//search google
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);
[3] ToggleButton的施用有感
来源: 互联网 发布时间: 2014-02-18
ToggleButton的使用有感
今天用到ToggleButton开关按钮,由于给他设置错了监听事件,效果不是所想 原来用的多的还是监听状态的改变。写一个例子,显示效果才明白。
其中对应的配置文件是
今天用到ToggleButton开关按钮,由于给他设置错了监听事件,效果不是所想 原来用的多的还是监听状态的改变。写一个例子,显示效果才明白。
package com.rotunda.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class ToggleButtonTest extends Activity {
/** Called when the activity is first created. */
private ToggleButton tg;
private TextView tv,tvc;
private static boolean ischecked=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tg=(ToggleButton)findViewById(R.id.togglebutton_displaychart_showgrid);
tv=(TextView)findViewById(R.id.textview_main_showtest);
tvc=(TextView)findViewById(R.id.textview_main_showclick);
// tg.setOnCheckedChangeListener(tgcheckedlistener);
tg.setOnClickListener(tgclicklistener);
// this.p
}
ToggleButton.OnClickListener tgclicklistener=new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
if(ischecked)
{
tvc.setText("click ischecked");
}
else
{
tvc.setText("click not checked");
}
}
};
ToggleButton.OnCheckedChangeListener tgcheckedlistener=new ToggleButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
tv.setText("checked");
ischecked=true;
System.out.println("ischecked==="+ischecked);
}
else
{
tv.setText("not checked");
ischecked=false;
System.out.println("ischecked==="+ischecked);
}
}
};
}其中对应的配置文件是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textview_main_showtest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textview_main_showclick"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ToggleButton
android:id="@+id/togglebutton_displaychart_showgrid"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:textOff="网格"
android:textOn="网格"
/>
</LinearLayout>
可以试着观察一下来了解
最新技术文章: