@SuppressLint or @TargetApi

@TargetApi and @SuppressLint have the same core effect: they suppress the Lint error.

The difference is that with @TargetApi, you declare, via the parameter, what API level you have addressed in your code, so that the error can pop up again if you later modify the method to try referencing something newer than the API level cited in @TargetApi.

Having @TargetApi(11) means that if Lint detects that I am using something newer than my android:minSdkVersion, but up to API Level 11, Lint will not complain. In this case, that works. If, however, I modified this method to reference something that wasn't added until API Level 14, then the Lint error would appear again, because my @TargetApi(11) annotation says that I only fixed the code to work on API Level 11 and below, not API Level 14 and below.


Using @SuppressLint('NewApi'), I would lose the Lint error for any API level, regardless of what my code references and what my code is set up to handle.

Hence, @TargetApi is the preferred annotation, as it allows you to tell the build tools "OK, I fixed this category of problems" in a more fine-grained fashion.

因此,“targetapi是最好的选择,因为它允许你告诉编译工具“OK,我用一个更精确的方式, 修正了这类问题”



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 这个类主要包括五个点击事件,分别为 * 1,ListView的长按点击事件,用来AlertDialog来判断是否删除数据。 * 2,ListView的点击事件,跳转到第二个界面,用来修改数据 * 4,menu里的退出事件,用来退出程序 * 5,menu里的新建事件,用来新建便签 * 6,对返回按钮监听退出程序 */ public class MainActivity extends Activity { // 定义帮助类 private MyOpenHelper notesDB; // 定义数据库类 private SQLiteDatabase dbReader;// 读取 // 定义组件 private ListView lv; private Button bt; private TextView tvBody; private TextView tvTitle; // 数据库表名称 private static final String TABLE_NAME = "data"; // 定义选项菜单 private static final int DB_ISNERT = 0x11; private static final int EXIT = 0x12; // 定义上下文菜单 private static final int DB_DELETE = 0x22; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.button1); // 实例化数据库对象 notesDB = new MyOpenHelper(this); // 创建数据库 dbReader = notesDB.getWritableDatabase(); // 取出组件 lv = (ListView) findViewById(R.id.listView1); // 设置监听 // 注册ListView上下文菜单 this.registerForContextMenu(lv); // 一打开软件就进行数据库查询把数据库中的内容查询显示 queryData(); // 短按时跳到第二个界面进行查看,修改 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Cursor c = dbReader.query(TABLE_NAME, null, null, null, null, null, null); c.moveToPosition(position);// 移动光标到一个绝对的位置 Intent i = new Intent(MainActivity.this, SecondAtivity.class); String s = String.valueOf(id); i.putExtra(SecondAtivity.mRowId, s); i.putExtra(SecondAtivity.KEY_BODY, c.getString(c.getColumnIndex(SecondAtivity.KEY_BODY))); i.putExtra(SecondAtivity.KEY_TITLE, c.getString(c.getColumnIndex(SecondAtivity.KEY_TITLE))); // 把数据库中的内容传到SecondAtivity startActivityForResult(i, 1); } }); } /** * 填充ListView */ public void queryData() { // 游标 查询数据库 Cursor cursor = dbReader.query(TABLE_NAME, null, null, null, nul

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值