package com.bawei.xutils3.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.xutils3.R;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
@ViewInject(R.id.tv)
TextView tv;
@ViewInject(R.id.iv)
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
x.view().inject(this);
tv.setText("这是我使用xutils3的注解方式");
}
//默认的是点击事件
/*1,方法必须私有限定,
2,方法参数形式必须和type对应的Listener接口一致.
3,注解参数value支持数组: value={id1, id2, id3}*/
/**
* 单击事件
* type默认View.OnClickListener.class,故此处可以简化不写,@Event(R.id.bt_main)
*/
// @Event(type = View.OnClickListener.class, value = R.id.tv)
@Event({R.id.tv, R.id.iv})
private void testOnClick(View view) {
switch (view.getId()) {
case R.id.tv:
Toast.makeText(MainActivity.this, "这是我使用注解方式实现点击事件", Toast.LENGTH_SHORT).show();
getData();
break;
case R.id.iv:
Toast.makeText(MainActivity.this, "这是我使用注解方式实现点击事件", Toast.LENGTH_SHORT).show();
getServerData();
break;
}
}
/**
* 长按事件
*
* @param v
* @return
*/
@Event(type = View.OnLongClickListener.class, value = R.id.iv)
private boolean testOnLongClick(View v) {
switch (v.getId()) {
case R.id.iv:
Toast.makeText(MainActivity.this, "这是我使用注解方式实现长按事件", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
//获取网络数据 缓存
private void getData() {
String url = "http://www.93.gov.cn/93app/data.do";
RequestParams params = new RequestParams(url);
params.addQueryStringParameter("channelId", "0");
params.addQueryStringParameter("startNum", "0");
params.setCacheMaxAge(1000 * 60);
x.http().get(params, new Callback.CacheCallback<String>() {
private String result = null;
@Override
public boolean onCache(String result) {
this.result = result;
//默认false不走缓存 true 走缓存 过期之后就不走缓存
Toast.makeText(MainActivity.this, "走缓存", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onSuccess(String result) {
if (result != null) {
this.result = result;
}
Log.i("aaa", result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
//获取网络数据
private void getServerData() {
String url = "http://www.93.gov.cn/93app/data.do";
RequestParams params = new RequestParams(url);
params.addQueryStringParameter("channelId", "0");
params.addQueryStringParameter("startNum", "0");
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Log.i("xxx", result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.xutils3.R;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
@ViewInject(R.id.tv)
TextView tv;
@ViewInject(R.id.iv)
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
x.view().inject(this);
tv.setText("这是我使用xutils3的注解方式");
}
//默认的是点击事件
/*1,方法必须私有限定,
2,方法参数形式必须和type对应的Listener接口一致.
3,注解参数value支持数组: value={id1, id2, id3}*/
/**
* 单击事件
* type默认View.OnClickListener.class,故此处可以简化不写,@Event(R.id.bt_main)
*/
// @Event(type = View.OnClickListener.class, value = R.id.tv)
@Event({R.id.tv, R.id.iv})
private void testOnClick(View view) {
switch (view.getId()) {
case R.id.tv:
Toast.makeText(MainActivity.this, "这是我使用注解方式实现点击事件", Toast.LENGTH_SHORT).show();
getData();
break;
case R.id.iv:
Toast.makeText(MainActivity.this, "这是我使用注解方式实现点击事件", Toast.LENGTH_SHORT).show();
getServerData();
break;
}
}
/**
* 长按事件
*
* @param v
* @return
*/
@Event(type = View.OnLongClickListener.class, value = R.id.iv)
private boolean testOnLongClick(View v) {
switch (v.getId()) {
case R.id.iv:
Toast.makeText(MainActivity.this, "这是我使用注解方式实现长按事件", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
//获取网络数据 缓存
private void getData() {
String url = "http://www.93.gov.cn/93app/data.do";
RequestParams params = new RequestParams(url);
params.addQueryStringParameter("channelId", "0");
params.addQueryStringParameter("startNum", "0");
params.setCacheMaxAge(1000 * 60);
x.http().get(params, new Callback.CacheCallback<String>() {
private String result = null;
@Override
public boolean onCache(String result) {
this.result = result;
//默认false不走缓存 true 走缓存 过期之后就不走缓存
Toast.makeText(MainActivity.this, "走缓存", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onSuccess(String result) {
if (result != null) {
this.result = result;
}
Log.i("aaa", result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
//获取网络数据
private void getServerData() {
String url = "http://www.93.gov.cn/93app/data.do";
RequestParams params = new RequestParams(url);
params.addQueryStringParameter("channelId", "0");
params.addQueryStringParameter("startNum", "0");
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Log.i("xxx", result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
}
//初始化
package com.bawei.xutils3.app;
import android.app.Application;
import org.xutils.x;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/3/8 15:55
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化xutils3
x.Ext.init(this);
//输出日志 设置为false就是为了不影响性能
x.Ext.setDebug(false);
}
}