你好,游客 登录 注册 搜索
背景:
阅读新闻

Android程序ToDoList增加配置项页面

[日期:2013-09-15] 来源:Linux社区  作者:yjf512 [字体:大 中 小]

2 Intent和Activity

Android应用程序的三种核心的组件:Activity,Service, Brocast Receiver。这三种组件互相或者内部进行交互的消息就叫做Intent。比如在我们这个程序中,就是两个Activity需要进行交互,这个时候就需要使用到了Intent了。

Intent有三种用法:

传递给Activity:startActivity(Intent), startActivityForResult()

传递给Service:startService(Intent), bindService()

传递给Broadcast:sendBroadcast(), sendOrderedBroadcast(), sendStickyBroadcast()

在这个例子中,除了从ToDoListActivity跳到Reference,也有从Reference跳到ToDoListActivity(点击返回按钮)。

Button cancelReference = (Button)findViewById(R.id. cancel);

          cancelReference.setOnClickListener( new View.OnClickListener() {

               

                @Override

                public void onClick(View v) {

                    Intent intent = new Intent();

                intent.setClass(Reference.this, ToDoListActivity.class);

                startActivity( intent);

                }

          });

3 配置项存储

关于配置项存储这里使用的是SharePreferences。SharePreferences提供了一个接口让你能存储和获取持久化的key-value数据。你可以持久化的数据类型有:boolean,float,int,long,string。

简单来说:

创建对象使用方法:

getSharePreferences()

getPreferences()

写数据使用方法:

1 使用edit()获取写句柄

2 调用putXXXX()方法

3 调用commit

读数据使用方法:

getXXX()

在这个例子中有这样用到:

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            boolean isSaveLocal = prefs.getBoolean( IS_SAVE_LOCAL, false);

 

...

 

Editor editor = prefs.edit();

                    editor.putBoolean( IS_SAVE_LOCAL, isChecked);

                    editor.commit();

总结

代码在https://github.com/jianfengye/Android_Works/tree/master/ToDoList

增加配置项也大都选择SharePreferences作为存储方法。至于Android还有其他的存储方法比如:

Internal Storage

External Storage

SQLite Databases

Network Connection

(参考http://developer.android.com/guide/topics/data/data-storage.html)

应该在存储代办事项的时候用得到

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

linux
相关资讯       Android程序  ToDoList 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款