当前位置:  编程技术>移动开发
本页文章导读:
    ▪设立ToggleButton、Switch、CheckBox和RadioButton的显示效果        设置ToggleButton、Switch、CheckBox和RadioButton的显示效果ToggleButton、Switch、CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似。CompoundButton有两个状态.........
    ▪ Gson解析(实战开发2)        Gson解析(实战开发二)闲话少扯,直接代码,一看就明白了: 【MainActivity】 package com.example.testgson; import java.io.StringReader; import com.google.gson.stream.JsonReader; import android.os.Bundle; import android.os..........
    ▪ block一点也不神秘————怎么利用block进行回调       block一点也不神秘————如何利用block进行回调我们在开发中常常会用到函数回调,你可以用通知来替代回调,但是大多数时候回调是比通知方便的,所以何乐而不为呢?如果你不知道回调.........

[1]设立ToggleButton、Switch、CheckBox和RadioButton的显示效果
    来源: 互联网  发布时间: 2014-02-18
设置ToggleButton、Switch、CheckBox和RadioButton的显示效果
ToggleButton、Switch、CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似。CompoundButton有两个状态,分别是checked和not checked。Switch是android4.0后出现的控件。但是这几个组件的默认图标都不太好看,怎样设置自己的图标风格呢?以下就是我的一种。

先看一下效果图,如下:



按钮图片贡献如下:

      

实现过程:

1.建立/res/drawable/setting_checkbox_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/on_button" android:state_checked="true"/>
    <item android:drawable="@drawable/off_button" android:state_checked="false"/>
</selector>

2、在values/styles.xml中添加如下代码:

 <style name="MyToggleButton" parent="@android:style/Widget.CompoundButton">
        <item name="android:button">@drawable/setting_checkbox_button</item>
    </style>

3.layout布局文件/res/layout/togglebutton_switch2.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layout1"
    android:gravity="center_horizontal">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="toggle is checked..." />

    <ToggleButton
        android:id="@+id/togglebutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:background="#00000000"
        
        android:checked="true"
        android:textOff="关闭"
        android:textOn="打开" />

     <Switch
        android:id="@+id/witch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@id/togglebutton"
        android:layout_alignLeft="@id/togglebutton"
        android:checked="true"
        android:textOff="关闭"
        android:textOn="打开"
        android:thumb="@drawable/tb_thumb"
        android:track="@drawable/tb_track" />
    
    
    <CheckBox
        android:id="@+id/check"
        
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/witch"
        android:checked="true"
        android:textOff="关闭"
        android:textOn="打开" />

    <RadioButton
        android:id="@+id/redio"
        
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/check"
        android:checked="false"
        android:textOff="关闭"
        android:textOn="打开" />

    <ToggleButton
        android:id="@+id/togglebutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/compound_button"
        android:layout_below="@id/redio"
        android:layout_alignLeft="@id/redio"
        android:visibility="gone"/>
    
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/redio"
        android:layout_gravity="center" 
        android:layout_alignLeft="@id/redio">

        <Switch
            android:id="@+id/witch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:checked="true"
            android:textOff="关闭"
            android:textOn="打开"/>
    </FrameLayout>

</RelativeLayout>

以上写的还有一问题,就是最后控件RadioButton点不了;还有就是把android:layout_width="wrap_content"改成android:layout_width="match_parent"布局会出错。可能这个方法还不太好,我也是在学习中,欢迎大家一起讨论,有更好的实现方式请一定告诉我哦!


有一个网友写的可以整个项目控制ToggleButton的风格,我看了一下,第2步设置Style & Theme的地方看不太懂,写/res/drawable/themes.xml这个文件时在我这里会报错。大家可以参考一下:http://blog.csdn.net/billpig/article/details/6634481



    
[2] Gson解析(实战开发2)
    来源: 互联网  发布时间: 2014-02-18
Gson解析(实战开发二)

闲话少扯,直接代码,一看就明白了:

【MainActivity】

package com.example.testgson;

import java.io.StringReader;

import com.google.gson.stream.JsonReader;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity {

	private String jsonData="<Data message=\'\' jsondata=\'{\"ds\":[{\"ID\":\"1\",\"Name\":\"智能时代\",\"IPAddress\":\"192.16"
	 +"8.0.0\"}]}\'/>";
	private String sName,sID,sIP;
	private TextView mTitle;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		mTitle = (TextView) findViewById(R.id.title);
		String sJson = jsonData.substring(33);
		Log.d("MainActivity", sJson);
		try {
			JsonReader reader = new JsonReader(new StringReader(sJson));
			reader.beginArray();
			while (reader.hasNext()) {
				reader.beginObject();
				while (reader.hasNext()) {
					String tagName = reader.nextName();
					if (tagName.equals("ID")) {
						sID = reader.nextString();
					} else if (tagName.equals("Name")) {
						sName = reader.nextString();
					} else if (tagName.equals("IPAddress")) {
						sIP = reader.nextString();
					}
				}
				reader.endObject();
			}
			reader.endArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.d("MainActivity", "ID:"+sID+"--Name:"+sName+"--IPAddress:"+sIP);
		new Thread() {
			@Override
			public void run() {
				Looper.prepare();// 防止报异常
				super.run();
				try {
					handler.sendEmptyMessage(0);
				} catch (Exception e) {
					Log.v("exception", e.getMessage()+ "");
				}
			}
		}.start();
	}
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			int ret = msg.what;
			if (ret == 0) {
				mTitle.setText(sName);
			}
		}
	};
}

【main】XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bj"
    android:orientation="vertical" 
    android:gravity="center_horizontal">
    
<LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="40dp"
	    android:orientation="horizontal">
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_one"
	        android:layout_marginLeft="5dp"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_two"
	        android:layout_marginLeft="10dp"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_three"
	        android:layout_marginLeft="10dp"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/state_four"
	        android:layout_marginLeft="10dp" />
	</LinearLayout>
	<LinearLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:gravity="center_horizontal"
	    >
	    <TextView 
	        android:id="@+id/title"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="XXXXXXXX"
	        android:textColor="@android:color/white"
	        android:textSize="35sp"/>
	    </LinearLayout>
	    <LinearLayout 
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:orientation="horizontal"
	        android:layout_marginTop="10dp"
	        android:gravity="center">
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/model_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="设备模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	            android:layout_marginLeft="5dp"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/home_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="房屋模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	             android:layout_marginLeft="5dp"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/scene_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="场景模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	        <LinearLayout 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="vertical"
	            android:gravity="center"
	             android:layout_marginLeft="5dp"
	            >
	            <ImageView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:background="@drawable/high_btn"/>
	            <TextView 
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:text="搞基模式"
	                android:textSize="20sp"
	                android:textColor="@android:color/white"/>
	        </LinearLayout>
	    </LinearLayout>
</LinearLayout>


    
[3] block一点也不神秘————怎么利用block进行回调
    来源: 互联网  发布时间: 2014-02-18
block一点也不神秘————如何利用block进行回调

我们在开发中常常会用到函数回调,你可以用通知来替代回调,但是大多数时候回调是比通知方便的,所以何乐而不为呢?如果你不知道回调使用的场景,我们来假设一下:

1.我现在玩手机

2.突然手机没有电了

3.我只好让手机开始充电

4.充电的过程中我好无聊,我要去看电视,但是我不会一直看电视,我要等手机电充满了停止看电视,继续去玩手机

5.我开始看电视

6.手机电充好了,我听到手机响了一下,我不看电视了我继续去玩手机。


这个场景中哪里跟回调类似呢?哪里跟通知类似呢?其实我们可以认为手机充好电了通过回调的方式让我继续玩手机,也可以认为手机充好电了通知我可以继续玩手机,然后我主动继续玩手机。这里更像通知不像回调。但是换个思维想,如果手机本身没有回调机制,那他怎么能在恰好手机刚充满的时候响一下呢?

先不纠结这个问题,我们看看如果用block怎样来实现这样一个场景:

我们随便找个控制器写下以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSLog(@"我在玩手机");
    NSLog(@"手机没电了");
    [self chargeMyIphone];
    NSLog(@"我在看电视");
    
   

    
}
-(void)chargeMyIphone
{
    [NSThread sleepForTimeInterval:10];
    NSLog(@"电充好了");
}
注意 这里我用了NSTread sleep,这样会让我的主线程沉睡10秒钟,这个过程中我我真的可以一边看电视一边充电吗?




所以我们应该让充电的线程和我看电视的线程错开执行!这里我们就不开新线程了,就让他10秒之后再执行吧。模拟下:


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSLog(@"我在玩手机");
    NSLog(@"手机没电了");
    [self performSelector:@selector(chargeMyIphone:) withObject:Nil afterDelay:10];

    NSLog(@"我在看电视");
    
   

    
}
改一下这一句代码后,我们再看看:

2013-09-17 00:47:54.786 故事版应用[1013:a0b]我在玩手机

2013-09-17 00:47:54.787 故事版应用[1013:a0b]手机没电了

2013-09-17 00:47:54.787 故事版应用[1013:a0b]我在看电视

2013-09-17 00:48:04.799 故事版应用[1013:a0b]电充好了

看起来没多大问题,但是我们还没写完我们的场景呢,我们想充好电之后继续玩手机?所以我们写在哪儿呢?

如果直接放在看电视后面:

 NSLog(@"继续玩手机");

我们看看控制台:

2013-09-17 00:50:12.417 故事版应用[1029:a0b]我在玩手机

2013-09-17 00:50:12.418 故事版应用[1029:a0b]手机没电了

2013-09-17 00:50:12.419 故事版应用[1029:a0b]我在看电视

2013-09-17 00:50:12.419 故事版应用[1029:a0b]继续玩手机

2013-09-17 00:50:22.431 故事版应用[1029:a0b]电充好了

呵呵!电都没充好,你就直接继续玩了?所以这里应该是电充好以后 我们再继续玩手机?

那么该怎么做?我们可以写进充电函数里吗?

-(void)chargeMyIphone
{
    NSLog(@"电充好了");
     NSLog(@"继续玩手机");
}

我们看看控制台:

2013-09-17 00:51:43.832 故事版应用[1044:a0b]我在玩手机

2013-09-17 00:51:43.833 故事版应用[1044:a0b]手机没电了

2013-09-17 00:51:43.833 故事版应用[1044:a0b]我在看电视

2013-09-17 00:51:53.848 故事版应用[1044:a0b]电充好了

2013-09-17 00:51:53.849 故事版应用[1044:a0b]继续玩手机

看起来没多大问题啊!但是我们想想看,我们把继续玩手机这件事情写在了充电函数的最后?

如果说我每次冲完电都继续玩手机,这个没什么问题!但是如果我每次冲完电之后不总是玩手机怎么办呢?

比如有一次我充完电之后想出门逛街!这个很好理解吧,那么这样写就不对了!我们想让充电函数最后执行的那一行是可以变化的。有很多方式可以做到,但是这里最好的做法肯定是追加一个block替换掉我们写死的那句代码啦!

也就是说我在充电前已经安排好一个充电后的计划了,今天冲完电继续玩手机,明天冲完电出门逛街,那么我每次调用的还是那个充电函数,只是传的参数不一样而已!我们不用传int 1表示玩手机,2表示出门逛街,我们直接把这两件事情当做参数传过去!

明白这一点就知道block的最终奥义啦!所以我们该改造一下这个函数,为他加个包含代码的参数!

-(void)chargeMyIphone:(void(^)(void))finishBlock
{
      NSLog(@"电充好了");
    //NSLog(@"继续玩手机");
    finishBlock();
}

这个追加的参数就是我们的block了,第一个void表示此block无返回值。(^)为block type的标志。第二个(void)表示这个block无参数。finishBlock就是他的名字。无参数无返回类型的匿名函数就是我们的最简单的block了!他非常方便我们用来回调,因为他没有返回值,没有参数,就相当于只有内部的可执行代码!

而我们将一个固定的事件用一段代码作为参数传了进来,并且以  名字() 形式来触发它,那么这个函数的结尾就不会总是玩手机了!那么他可以是任何事情!

所以我们来尝试调用下这个函数:


但是这里的参数我们该怎么传呢?我们通过这种方式传block参数好像不符合他这的object,所以我们还是直接调吧,但是把这一行用dispatch_after包起来~


按一下回车:


然后在中间写我要做什么,这里是出门逛街;


接下来在chargeIphone内部敲dis,然后联想出来选择第一个按回车:


然后将时间改为10,把整个方法内的代码移动进去:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSLog(@"我在玩手机");
    NSLog(@"手机没电了");
   
    [self chargeMyIphone:^{
        NSLog(@"出去逛街");
    }];
    NSLog(@"我在看电视");
}
-(void)chargeMyIphone:(void(^)(void))finishBlock
{
    double delayInSeconds = 10.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"电充好了");
        finishBlock();
    });
    
}

现在代码的结构非常清晰:

充电完成之后 我要去逛街。

充电内部需要耗时10秒。

充电的同时我可以看电视。

充电完成之后回头来触发我block中设置的出去逛街。


这样设置block的好处我已经说过了,我们没有把充电函数内部的实现写死,也就是说当我完成之后无论做什么都无所谓,调的地方不同,传不同的代码过去就可以了,这个跟函数指针类似吧。

我们来运行一下程序验证下最后的结果:

2013-09-17 01:17:23.127 故事版应用[1088:a0b]我在玩手机

2013-09-17 01:17:23.129 故事版应用[1088:a0b]手机没电了

2013-09-17 01:17:23.129 故事版应用[1088:a0b]我在看电视

2013-09-17 01:17:33.130 故事版应用[1088:a0b]电充好了

2013-09-17 01:17:33.131 故事版应用[1088:a0b]出去逛街


完全验证了我们的结论,23秒开始充电 ,看电视,33秒的适合充好电出去逛街。

OK ,最简单的block我们就讲到这里,用好他来给你的代码非写死吧!DON'T HARD WRITE




    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3