SampleWhereDoYouLivex - DNNVietNam

kingfishblacksmithMobile - Wireless

Dec 14, 2013 (3 years and 10 months ago)

157 views

Sample: Where do you live?




1.

Khai báo trong file manifest

Listing 1.6 AndroidManifest.xml for the Where Do You Live application

<?xml version="1.0" encoding="utf
-
8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="
com.msi.manning.unlockingandroid">


<application android:icon="@drawable/icon">

<activity android:name=".AWhereDoY
ouLive" android:label="@string/
app_name">

<intent
-
filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android
.intent.category.LAUNCHER" />

</intent
-
filter>

</activity>

</application>

</manifest>


2.

Implementing the Android Activityin AWhereDoYouLive.java

Listing 1.7 Implementing the Android Activityin AWhereDoYouLive.java

package com.msi.manning.unlockingandroid;

// imports omitted for brevity

public class AWhereDoYouLive extends Activity {

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

final EditText addressfield = (EditText) findViewById(R.id.address);

fina
l Button button = (Button) findViewById(R.id.launchmap);


button.setOnClickListener(new Button.OnClickListener() {

public void onClick(View view) {

try {

String address = addressfield.getText().toString();

address = address.replace(' ', '+');

Intent geoI
ntent = new
Intent(android.content.Intent.ACTION_VIEW,

Uri.parse("geo:0,0?q=" + address));

startActivity(geoIntent);

}

catch (Exception e) {



}

}

});

}

}


Listing 1.8 R.java contains the Rclass, which has UI element identifiers

/* AUTO
-
GENERATED
FILE. DO NOT MODIFY.

*

* This class was automatically generated by the

* aapt tool from the resource data it found. It

* should not be modified by hand.

*/

package com.msi.manning.unlockingandroid;

public final class R {

public
static final class attr {
}

p
ublic static final class drawable {

public static final int icon=0x7f020000;

}

public static final class id {

public static final int address=0x7f050000;

public static final int launchmap=0x7f050001;

}

public static final class layout {

public

static final int main=0x7f030000;

}

public static final class string {

public static final int app_name=0x7f040000;

}

}



Main.xml defines the UIelements for our sample application

<?xml version="1.0" encoding="utf
-
8"?>

<LinearLayout xmlns:android="http:/
/schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="P
lease enter y
our home address."
/>

<EditText

android:id="@+id/address"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:autoText="true"/>

<Button

android:id="@+id/launchmap"

android:layout_width="wrap_content"

android:layout_height="
wrap_content"

android:text="Show Map"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Unlocking Android, Chapter 1." />

</LinearLayout>


Listing 1.10 strings.xml

<?xml version="1.0" encoding="utf
-
8"?>

<resources>

<string name="app_name">Where Do You Live</string>

</resources>