Android Annotations框架第2篇-实例代码1

常用注释的使用方法:

https://github.com/excilys/androidannotations/wiki/AvailableAnnotations


jar包作用和打包混淆

  • androidannotation-xxx.jar是工程编译文件

  • androidannotations-api-xxx.jar是api调用文件

  • 混淆打包请在proguard-project.txt添加:

dontwarn org.springframework.**

Example实例说明

  • MyActivity类
package org.androidannotations.helloworldeclipse;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.LongClick;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.annotations.Touch;
import org.androidannotations.annotations.Transactional;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.BooleanRes;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@EActivity(R.layout.my_activity)
// 加载Activity视图
public class MyActivity extends Activity {

  @ViewById
  // 获取组件id为myEditText,如果不写id,则默认是变量名称
  EditText myEditText;

  @ViewById(R.id.myTextView)
  // 获取组件id为myEditText
  TextView textView;

  @StringRes(R.string.hello)
  // 获取values.xml中的string资源--><string name="hello">Hello %s!</string>
  String helloFormat;

  @ColorRes
  int androidColor;// 获取values.xml中颜色值--><color
                   // name="androidColor">#A4C639</color>

  @BooleanRes
  boolean someBoolean;// 获取values.xml中bool值--> <bool
                      // name="someBoolean">true</bool>

  @SystemService
  // 获取系统提供的Manager
  NotificationManager notificationManager;

  @SystemService
  // 获取系统提供的Manager
  WindowManager windowManager;

  /**
   * 按返回键 <br>
   * AndroidAnnotations gracefully handles support for onBackPressed, whether
   * you use ECLAIR (2.0), or pre ECLAIR android version.
   */
  public void onBackPressed() {
    Toast.makeText(this, "Back key pressed!", Toast.LENGTH_SHORT).show();
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // windowManager should not be null
    windowManager.getDefaultDisplay();// 获取默认的显示对象
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);// 不确定的进度
  }

  @Click
  // id为myButton的点击事件
  void myButtonClicked() {
    String name = myEditText.getText().toString();
    setProgressBarIndeterminateVisibility(true);
    someBackgroundWork(name, 5);
  }

  @Background
  // 开启新线程后台运行,注意不要引用UI控件,而且返回值类型一定是void
  void someBackgroundWork(String name, long timeToDoSomeLongComputation) {
    // 模拟后台耗时事件
    try {
      TimeUnit.SECONDS.sleep(timeToDoSomeLongComputation);
    } catch (InterruptedException e) {
    }

    String message = String.format(helloFormat, name);

    // 更新UI
    updateUi(message, androidColor);

    // 显示状态栏通知
    showNotificationsDelayed();
  }

  @UiThread
  // UI线程,用于更新UI
  void updateUi(String message, int color) {
    setProgressBarIndeterminateVisibility(false);
    textView.setText(message);
    textView.setTextColor(color);
  }

  @UiThread(delay = 2000)
  // 显示状态栏通知,设置延时时间,以毫秒为单位
  void showNotificationsDelayed() {
    Notification notification = new Notification(R.drawable.icon, "Hello !", 0);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello World!", contentIntent);
    notificationManager.notify(1, notification);
  }

  @LongClick
  // id为startExtraActivity的按钮长按事件
  void startExtraActivity() {
    Intent intent = ActivityWithExtra_.intent(this).myDate(new Date()).myMessage("hello !").get();
    intent.putExtra(ActivityWithExtra.MY_INT_EXTRA, 42);
    startActivity(intent);
  }

  @Click
  // id为startListActivity的按钮点击事件
  void startListActivity(View v) {
    startActivity(new Intent(this, MyListActivity_.class));
  }

  @Touch
  // id为myTextView的文本触摸事件
  void myTextView(MotionEvent event) {
    Log.d("MyActivity", "myTextView was touched!");
  }

  @Transactional
  int transactionalMethod(SQLiteDatabase db, int someParam) {
    return 42;
  }
}
  • my_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click me!" />

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/startExtraActivity"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start extra activity, long click !" />

    <Button
        android:id="@+id/startListActivity"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start list activity !" />

</LinearLayout>
  • values.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AA-sample-HelloWorldEclipse</string>
    <string name="hello">Hello %s!</string>

    <color name="androidColor">#A4C639</color>

    <string-array name="bestFoods">
        <item>Foie gras</item>
        <item>pez espada</item>
        <item>california maki</item>
    </string-array>

    <bool name="someBoolean">true</bool>

</resources>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值