您的位置:首页 > 编程语言 > Java开发

二 TitleEditor.java学习笔记

2015-08-26 19:29 507 查看
Java代码结构:

TitleEditor extends Activity:

        --onCreate()

        ---onPause()

       ---onResume()

       --onClickOk()

其中onCreate,onPause,onResume均为Activity类中方法的实现。

onClickOk是哪儿来的呢,查看代码知道布局文件使用了R.layout.title_editor;

查看布局文件res\layout\title_editor.xml可知,

<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/button_ok"
android:onClick="onClickOk" />有一个按钮绑定了onCLick事件,调取了onClickOk函数。这里有点像html里调用js函数的情形。
一个活动启动,可以获得的两个参数,一个是intent,另外一个是category。

intent参数的获取可用getIntent().getData(),用来传递活动需要的数据;而category就是Mainfest.xml文件中绑定的<category >标签,在java文件中的使用形式如下

Intent.ACTION_Name.equals(intent.getAction());

一般是用来说明该活动是干什么的。

一:onCreate()

该函数是活动被创建时运行的函数,定义了一些初始化的操作。

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title_editor);//使用布局文件
mUri = getIntent().getData();
//获取intent参数,在这里传递的参数是URI形式的,就是那个content://.../..

mCursor = managedQuery(
mUri, // The URI for the note that is to be retrieved.
PROJECTION, // The columns to retrieve
null, // No selection criteria are used, so no where columns are needed.
null, // No where columns are used, so no where values are needed.
null // No sort order is needed.
);
//manageQuery函
9b51
数,是用来查询数据的一个函数,具体参数说明已经很清楚了。在这儿mUri是URI,PROJECTION是一个字符串数组,表明要查询那些数据。
//返回的是一个cursor,这东西比较奇怪,据说是sqlite特有的,cursor作为查询结果并不是数组形式的,要想获取数据要使用Cursor类中自带的几个方法比如
//getString(columnIndex),getInt(columnIndex)之类的,getColumnIndex(columnName),getColumnCount(),getColumnNames()。要注意Cursor读取数据后指向下一条数据
//move(int offset)等函数可以调整Cursor指向哪一个数据
 // Gets the View ID for the EditText box
mText = (EditText) this.findViewById(R.id.title);
//这个函数是用来获取view id的,类似于句柄的概念。表明是哪个UI元素。
//R.id.title是什么意思呢,在启动活动时android 系统会分配id给UI元素,
//<EditText android:id="@+id/<strong>title</strong>"
R.id.title的意思是就是指上面的<EditText>
 }
二:onResume()

onResume是活动被创建之后,看到之前执行。

protected void onResume() {
super.onResume();
if (mCursor != null) {
mCursor.moveToFirst();//这里表示读取第一条数据
// Displays the current title text in the EditText object.
mText.setText(mCursor.getString(COLUMN_INDEX_TITLE));//把<EditText>中的文本设为查询到的文本
}
}


三:onPause()
onPause是活动被隐藏时调用的,就是切屏切到另外一个程序或切到主界面是调用的。

protected void onPause() {
super.onPause();

if (mCursor != null) {
ContentValues values = new ContentValues();

values.put(NotePad.Notes.COLUMN_NAME_TITLE, mText.getText().toString());
//类似于json字符串的东西,这儿的意思是取得<EditText>中的文本,与前面参数形成键值对,放到value里<pre name="code" class="html"> //形成 NotePad.Notes.COLUMN_NAME_TITLE=mText.getText().toString() 的形式(左右都是字符串)

 Log.v("TitleEditor",NotePad.Notes.COLUMN_NAME_TITLE); Log.v("TitleEditor",values.toString());

//Log.v(TAG,CONTENT)写日志,调试时可用logcat查看返回的值,TAG是LOG TAG,CONTENT是日志内容

//Log.w,Log.e,等也差不多,不过Log.w是warning,Log.e是错误,Log.d是debug信息

getContentResolver().update( mUri, // The URI for the note to update. values, // The values map containing the columns to update and the values to use. null, // No selection criteria is used, so no "where" columns are needed. null // No "where" columns are
used, so no "where" values are needed. ); //这个函数是更新数据用的,跟manageQuery()可以说是一对。 } }


四:onClick()
finish();
结束当前活动,但并不调用onDestroy()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: