AnnotationsFramework注解框架的使用(一)

Annotations Framework 是一款注解框架,让我们省去了很多的代码,很好用,也可以注解方法,让方法运行在工作线程,或者主线程。
使用方法:
@ViewById(R.id.editText1) //对控件的注解
@Click(R.id.button1) //onClick 方法的注解。
@Background //对耗时操作方法的注解
@UiThread //对更新UI的方法的注解。

DEMO:在MainActivity 中点击登录按钮
一,androidannotations-api-3.3.2.jar 这个是AnnotationsFramework的jar包。把jar包复制粘贴到Eclipse创建的android的libs文件夹中。
二,layout 中的布局文件 activity_main.xml

 <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="23dp"
        android:text="TextView" />

三,新建LoginBiz 类

public class LoginBiz {
//login 方法 需要传入MainActivity对象,与username
    public void login(MainActivity mainActivity, String username) {
        //当前线程ID是多少
        int thread = (int) Thread.currentThread().getId();
        if ("a".equals(username)) {
        // 如果用户名是a 登录成功
            mainActivity.showResult(true);
        }
    }
}

四,MainActivity中注解框架的使用

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;


//对MainActivity的注解
@EActivity(resName="activity_main")
public class MainActivity extends Activity {
    //对EditText 的注解
    @ViewById(R.id.editText1)
    EditText etUsername;

    //对TextView 的注解
    @ViewById(R.id.textView1)
    TextView tvResult;

    // 点击button1,执行login方法
    @Click(R.id.button1)
    public void login()
    {
        int threaId=(int) Thread.currentThread().getId();
        String username=etUsername.getText().toString();
        callBiz(username);

    }

    //执行业务,让方法运行在工作线程中
    @Background
    public void callBiz(String username)
    {
        int threaId=(int) Thread.currentThread().getId();

        LoginBiz biz=new LoginBiz();
        biz.login(this,username);
    }


    //更新UI,让方法运行在主线程中
    @UiThread
    public void  showResult(boolean isSuccerss)
    {
        int threaId=(int) Thread.currentThread().getId();
        if (isSuccerss)
        {
        tvResult.setText("成功");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值