zoukankan      html  css  js  c++  java
  • Andorid之Annotation框架初使用(四)

    代替繁琐的finViewById

    @EActivity
    public class MyActivity extends Activity {
      // Injects R.id.myEditText
      @ViewById
      EditText myEditText;
      @ViewById(R.id.myTextView)
      TextView textView;
    }


    指定需要在视图加载完成后才能执行的方法@AfterViews

    @EActivity(R.layout.main)
    public class MyActivity extends Activity {
        @ViewById
        TextView myTextView;
        @AfterViews
        void updateTextWithDate() {
            myTextView.setText("Date: " + new Date());
        }
    }

    注意:不要在onCreate中写任何对view相关的方法


    @Extra 用于传递的Intent

    @EActivity
    public class MyActivity extends Activity {
      @Extra("myStringExtra")
      String myMessage;
      @Extra("myDateExtra")
      Date myDateExtraWithDefaultValue = new Date();
       // The name of the extra will be "myMessage"
      @Extra
      String myMessage;
    }


    onNewIntent能够根据Intent重新注入Extra

    @EActivity
    public class MyActivity extends Activity {
        @Extra("myStringExtra")
        String myMessage;
        @Override
        protected void onNewIntent(Intent intent) {
            setIntent(intent);
        }
    }

     

    @SystemService
    no more Context.getSystemService()

    @EActivity
    public class MyActivity extends Activity {
      @SystemService
      NotificationManager notificationManager;
    }


    @SharedPref:

    定义:

    @SharedPref
    public interface MyPrefs {
            // The field name will have default value "John"
        @DefaultString("John")
        String name();
            // The field age will have default value 42
        @DefaultInt(42)
        int age();
            // The field lastUpdated will have default value 0
        long lastUpdated();
        @DefaultRes(R.string.defaultPrefName)
        String resourceName();
    
        @DefaultRes
        String defaultPrefAge();
    }


    使用:

    @EActivity
    public class MyActivity extends Activity {
        @Pref
        MyPrefs_ myPrefs;
    }
    
    // Simple edit
    myPrefs.name().put("John");
    
    // Batch edit
    myPrefs.edit()
      .name()
      .put("John")
      .age()
      .put(42)
      .apply();
    
    // Preference clearing:
    myPrefs.clear();
    
    // Check if a value exists:
    boolean nameExists = myPrefs.name().exists();
    
    // Reading a value
    long lastUpdated = myPrefs.lastUpdated().get();
    
    // Reading a value and providing a fallback default value
    long now = System.currentTimeMillis();
    long lastUpdated = myPrefs.lastUpdated().getOr(now);



     

  • 相关阅读:
    违反并发性: UpdateCommand 影响了预期1条记录中的0条——我的解决方案
    (转)使用DataGridView控件常见问题解答
    C#中如何去除数组中重复的项
    C#中如何去除HTML标记
    (转)中断基本概念
    JavaScript 操作 Cookie
    要有梦想创造卓越的职业生涯
    《暗时间》读书笔记与读后感
    前端攻略系列
    前端攻略系列(一) 前端各种优化(保证持续更新)
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3149905.html
Copyright © 2011-2022 走看看