安卓第六夜 凡高的自画像
作者:Vamei 出處:http://www.cnblogs.com/vamei 歡迎轉(zhuǎn)載,也請(qǐng)保留這段聲明。謝謝!
?
在上一講中,我已經(jīng)制作了一個(gè)簡(jiǎn)單的Android應(yīng)用。項(xiàng)目的主要文件包括:
- MainActivity.java
- activity_main.xml
在這一講,我將拓展應(yīng)用的功能,從而允許用戶輸入個(gè)人信息。
?
《自畫(huà)像》,凡高。凡高一生不得志,精神更是越來(lái)越差。在割掉自己的耳朵一部分后,畫(huà)家給自己留下了這幅自畫(huà)像。在當(dāng)時(shí),這幅畫(huà)依然是無(wú)人問(wèn)津。
?
描述
我將創(chuàng)建一個(gè)新的Activity。這個(gè)界面允許用戶輸入本人的姓名和博客地址。這些輸入數(shù)據(jù)將在會(huì)保存起來(lái)。在主界面中再次調(diào)出數(shù)據(jù),并顯示。相關(guān)知識(shí)點(diǎn)包括:
- 啟動(dòng)Activity。利用Intent,啟動(dòng)一個(gè)新的Activity。
- 文本輸入。增加EditText輸入欄,讓用戶輸入文本信息。
- 數(shù)據(jù)存儲(chǔ)。將數(shù)據(jù)存入SharedPreferences,跨Activity讀取。
?
創(chuàng)建新的項(xiàng)目文件
我們將在應(yīng)用項(xiàng)目中增加新的文件。在ADT的文件導(dǎo)航欄中右鍵點(diǎn)擊文件夾,選擇New->Other后,可以得到下面的對(duì)話框:
?
你可以在這里選擇想要?jiǎng)?chuàng)建的新文件的類(lèi)型,比如XML文件,Java文件等。安卓會(huì)根據(jù)你選擇的類(lèi)型,做一些默認(rèn)設(shè)置。比如選擇Android Activity后,在AndroidManifest.xml中自動(dòng)注冊(cè)該Activity,從而省去用戶的麻煩。?
?
新Activity的界面
我們要設(shè)計(jì)一個(gè)用于輸入姓名和博客地址的安卓界面。由于姓名和博客地址都是字符串類(lèi)型,因此我們可以借助EditText類(lèi)的View元素,來(lái)增加字符串類(lèi)型的輸入欄。我們還需要提交功能。按鈕是最容易想到的方式。這里,我沒(méi)有使用按鈕,而是為一個(gè)TextView增加點(diǎn)擊事件監(jiān)聽(tīng)。
?
這個(gè)視圖的XML布局文件為activity_self_edit.xml,如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin" ><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content" ><LinearLayoutandroid:id="@+id/input1"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Name" /><EditTextandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="full name" /></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/input1" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Blog Address" /><EditTextandroid:id="@+id/url"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="url" /></LinearLayout></LinearLayout><TextViewandroid:id="@+id/submit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:text="Submit" /> </RelativeLayout>這里使用了RelativeLayout來(lái)布局。我們可以在該布局下,說(shuō)明元素的相對(duì)位置。TextView就使用了layout_alignParentBottom屬性,來(lái)說(shuō)明它的下邊緣將對(duì)齊母元素的下邊緣。此外,這里還嵌套使用了LinearLayout。
?
用SharedPreferences存儲(chǔ)數(shù)據(jù)
我們現(xiàn)在來(lái)為視圖編寫(xiě)SelfEditActivity,以增加數(shù)據(jù)存儲(chǔ)功能。在提交數(shù)據(jù)后,這個(gè)Activity將通過(guò)EditText的getText()方法獲取字符輸入。提交的字符串將使用SharedPreferences存儲(chǔ)起來(lái)。
SharedPreferences以鍵值對(duì)(key-value pair)的方式存儲(chǔ)數(shù)據(jù)。在寫(xiě)入時(shí),我們需要說(shuō)明數(shù)據(jù)對(duì)應(yīng)的鍵。在讀取時(shí),我們將根據(jù)鍵,來(lái)獲得對(duì)應(yīng)的數(shù)據(jù)值。數(shù)據(jù)可以在整個(gè)應(yīng)用范圍內(nèi)調(diào)用。特定權(quán)限下,SharedPrefereces可以跨應(yīng)用使用。SharedPreferences簡(jiǎn)單易用,適合存儲(chǔ)少量的、結(jié)構(gòu)松散的信息。
package me.vamei.vamei;import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView;public class SelfEditActivity extends Activity {private EditText nameInput;private EditText urlInput;private TextView tvSubmit;private SharedPreferences sharedPref;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_self_edit);// find viewstvSubmit = (TextView) findViewById(R.id.submit);nameInput = (EditText) findViewById(R.id.name);urlInput = (EditText) findViewById(R.id.url);sharedPref = this.getSharedPreferences("me.vamei.vamei", Context.MODE_PRIVATE);// submittvSubmit.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String name = nameInput.getText().toString();String url = urlInput.getText().toString();// Save to Shared PreferencesSharedPreferences.Editor editor = sharedPref.edit();editor.putString("name", name);editor.putString("url", url);editor.commit();// End Current ActivitySelfEditActivity.this.finish();}});} }Context對(duì)象的getSharedPreferences()獲得SharedPrerences。第一個(gè)參數(shù)"me.vamei.vamei"說(shuō)明了該SharedPreferences對(duì)象所在的文件名,第二個(gè)參數(shù)說(shuō)明了權(quán)限,即只能該應(yīng)用范圍內(nèi)私用。使用SharedPreferences.Editor類(lèi)對(duì)象寫(xiě)入數(shù)據(jù)時(shí),我們調(diào)用了putString()方法,以存儲(chǔ)字符串類(lèi)型的數(shù)據(jù)值。commit()方法將執(zhí)行寫(xiě)入。
最后,Activity Context的finish()方法結(jié)束當(dāng)前Activity。
?
從一個(gè)Activity啟動(dòng)另一個(gè)Activity
我現(xiàn)在要把MainActivity和新的SelfEditActivity用Intent串聯(lián)起來(lái)。MainActivity對(duì)應(yīng)的布局為activity_main.xml,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent" ><TextView android:id="@+id/welcome"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/author"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Edit Profile" /> </LinearLayout>TextView用于顯示用戶信息。Button用于啟動(dòng)上面定義的SelfEditActivity。
?
修改MainActivity。相對(duì)于上一講,這里主要增加了兩個(gè)功能。一個(gè)功能是監(jiān)聽(tīng)按鈕,在按鈕點(diǎn)擊后啟動(dòng)SelfEditActivity。另一個(gè)功能是從SharedPreferences中獲得存儲(chǔ)的鍵值,即用戶名,再將用戶名信息更新到界面上。
package me.vamei.vamei;import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;public class MainActivity extends Activity {private SharedPreferences sharedPref;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);sharedPref = this.getSharedPreferences("me.vamei.vamei", Context.MODE_PRIVATE);Button btn = (Button) findViewById(R.id.author);btn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View view) {Intent intent = new Intent(MainActivity.this, SelfEditActivity.class);MainActivity.this.startActivity(intent);}});}@Overrideprotected void onResume() {super.onResume();TextView nameView = (TextView) findViewById(R.id.welcome);// retrieve content from shared preference, with key "name"String welcome = "Welcome, " + sharedPref.getString("name", "unknown") + "!";nameView.setText(welcome);} }Intent構(gòu)造器的第一個(gè)參數(shù)說(shuō)明了Context,第二個(gè)參數(shù)顯式的說(shuō)明了想要啟動(dòng)的功能單元,即SelfEditActivity類(lèi)。startActivity()方法將根據(jù)intent參數(shù),創(chuàng)建并啟動(dòng)對(duì)應(yīng)的功能單元。該方法執(zhí)行后,新的SelfEditActivity將壓入棧頂,成為激活的Activity,而MainActivity將暫停。
另一方面,我把數(shù)據(jù)讀取部分放入了onResume()方法。根據(jù)安卓的規(guī)定,MainActivity第一次運(yùn)行,以及每次從暫停中復(fù)蘇時(shí),都會(huì)調(diào)用該方法。用戶在SelfEditActivity修改返回后,就可以立即獲得最新的數(shù)據(jù)。使用getString()方法,從SharedPreferences對(duì)象獲得鍵位"name"的值。如果"name"鍵不存在,將返回默認(rèn)值"unknown"。
onResume()
?
最后,用setText()方法,把新的數(shù)據(jù)加載到視圖的TextView中。
?
運(yùn)行
在"Edit Profile"中輸入用戶信息,提交后返回主頁(yè)面,效果如下:
總結(jié)
RelativeLayout, EditText
Intent, startActivity()
getSharedPreferences(), putString, getString()
onResume()
?
歡迎繼續(xù)閱讀“Java快速教程”系列文章?
總結(jié)
以上是生活随笔為你收集整理的安卓第六夜 凡高的自画像的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 为什么超长列表数据的翻页技术实现复杂(二
- 下一篇: linux学习之路之使用脚本来复制二进制