Development of a small app for Android

Source: Internet
Author: User

Develop a particularly simple small Android app that mainly does these things:

1. Configuring the Activity_main.xml File

Placing a textview and a listview,textview for displaying text, the ListView setting ID asks Listview0 for placing multiple item;

The position (location) and ID (the first few lines) of item are starting from 0 (Onitemclick (adapterview<?> arg0, View view,int position, long ID))

2. New Listview0.xml file

Place a imageview to display the picture. This file layout is the layout of one item in the ListView above.

3. Configure the data source for the ListView

Since there is only one imageview inside the listview0.xml, the data is set to the following methods:

Private list<map<string, object>> GetData () {

list<map<string, object>> list = new arraylist<map<string, object>> ();
map<string, object> map = null;
int imgs[] = new int[] {r.drawable.buyno, R.drawable.quno};
for (int i = 0; i < imgs.length; i++) {
Map = new hashmap<string, object> ();
Map.put ("ImageView", Imgs[i]);
List.add (map);
}
return list;
}

The adapter configures the data into the ListView

Simpleadapter adapter = new Simpleadapter (this, GetData (),
R.LAYOUT.LISTVIEW01, new string[] {"ImageView"},
New int[] {r.id.imageview});
Listview.setadapter (adapter);

4. Item Click event in ListView:

Pass the item ID to the next avtivity with the start to ensure that clicking on different item responds to different events.

Listview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View ImageView,
int position, long ID) {
Intent Intent = new Intent ();
Intent.putextra ("position", Position + "");
Intent.setclass (Mainactivity.this, Choiceactivity.class);
StartActivity (Intent);
}
});

5. Configuration Menu: (Menu Development 1)

In the menu folder, configure the item in Main.xml and set the ID, which is used when clicking on item.

6. Menu click event:

Responds to different events based on the ID of item.

public boolean onoptionsitemselected (MenuItem item) {
int id = item.getitemid ();
if (id = = r.id.exit0) {
Exit
MainActivity.this.finish ();
return true;
}
return super.onoptionsitemselected (item);
}

7. Configure another activity's layout file Choice.xml

Place a ImageView and two buttons and center

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<imageview
Android:id= "@+id/imageview2"
android:src= "@drawable/think"
Android:layout_width= "Fill_parent"
android:layout_height= "300DP"
android:contentdescription= "@string/think"
android:layout_gravity= "Center"
/>

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:gravity= "Center"
android:orientation= "Horizontal" >

<button
Android:id= "@+id/choicebutton"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/chose"
android:layout_gravity= "Center_horizontal"/>

<button
Android:id= "@+id/exitbutton"
android:text= "@string/back"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>

</LinearLayout>

</LinearLayout>

8. Get data when starting another activity

Intent Intent = This.getintent ();
Position = Intent.getstringextra ("position");

9, according to different item click event to set different picture (according to Posotion to judge)

Fill picture
Private int[] Getimgs () {
int imgs[] = new INT[2];
if ("0". Equals (position)) {
Imgs[0] = r.drawable.buy;
IMGS[1] = r.drawable.nobuy;
}else if ("1". EndsWith (position)) {
Imgs[0] = R.drawable.go;
IMGS[1] = R.drawable.nogo;
}
return IMGs;
}

10. Click events for ImageView and Buttond

Click once to get pictures randomly

Convert picture
Private drawable changeimg (int[] imgs) {
Drawable drawable;
int num = (int) (Math.random ());
if (num>=0&&num<1) {
drawable = Getresources (). getdrawable (Imgs[0]);
}else if (num>=1&&num<2) {
drawable = Getresources (). getdrawable (R.drawable.think);
}else{
drawable = Getresources (). getdrawable (Imgs[1]);
}
return drawable;
}

Trunk:

Drawable drawable = null;
int[] IMGs = Getimgs ();
drawable = changeimg (IMGs);
Imageview.setimagedrawable (drawable);

10. Add menu and set Click Events (Menu Development 2)

Copy someone else's high-quality code below

Create a Menu
@Override
public boolean Oncreateoptionsmenu (Menu menu) {

/*
*
* The four parameters of the Add () method, in turn:
*
* 1, groups, if not grouped, then write Menu.none,
*
* 2, ID, this is very important, Android based on this ID to determine the different menu
*
* 3, order, the menu is now in front by the size of this parameter is determined
*
* 4, text, menu display text
*/

Menu.add (Menu.none, Menu.first + 1, 5, "pending development"). SetIcon (

Android. R.drawable.ic_menu_delete);

The SetIcon () method sets the icon for the menu, which is used by the system's own icon, students pay attention to

Android. R begins with resources that are provided by the system, and the resources we provide ourselves are those that start with R

Menu.add (Menu.none, Menu.first + 2, 2, "pending development"). SetIcon (

Android. R.drawable.ic_menu_edit);

Menu.add (Menu.none, Menu.first + 3, 6, "exit"). SetIcon (

Android. R.DRAWABLE.IC_MENU_HELP);

Menu.add (Menu.none, Menu.first + 4, 1, "Pending development"). SetIcon (

Android. R.drawable.ic_menu_add);

Menu.add (Menu.none, Menu.first + 5, 4, "pending development"). SetIcon (

Android. R.drawable.ic_menu_info_details);

Menu.add (Menu.none, Menu.first + 6, 3, "pending development"). SetIcon (

Android. R.drawable.ic_menu_send);

return true;
}

@Override
public boolean onoptionsitemselected (MenuItem item) {

Switch (Item.getitemid ()) {

Case Menu.first + 1:

Toast.maketext (This, "Pending development", Toast.length_long). Show ();

Break

Case Menu.first + 2:

Toast.maketext (This, "Pending development", Toast.length_long). Show ();

Break

Case Menu.first + 3:

ChoiceActivity.this.finish ();
Break

Case Menu.first + 4:

Toast.maketext (This, "Pending development", Toast.length_long). Show ();

Break

Case Menu.first + 5:

Toast.maketext (This, "Pending development", Toast.length_long). Show ();

Break

Case Menu.first + 6:

Toast.maketext (This, "Pending development", Toast.length_long). Show ();

Break

}
return true;
}

Pan out: Add shake to change the picture function:

First, you need to implement Interface Sensoreventlistener

1. Declaration of sensor management and vibrator

Sensor Management class
Private Sensormanager Sensormanager;
Vibrating device
Private vibrator Vibrator;

2. Access to System services

In the OnCreate function

Sensor
Sensormanager = (Sensormanager) getsystemservice (Sensor_service);
Vibration
Vibrator = (Vibrator) getsystemservice (Service.vibrator_service);

3, registered monitoring

Overloaded Onresume functions

Register for monitoring
Sensormanager.registerlistener (This, sensormanager.getdefaultsensor (Sensor.type_accelerometer),
Sensormanager.sensor_delay_normal);

4. Release the Monitor

Overloaded OnPause functions

Sensormanager.unregisterlistener (this);

5. Shake and shake response events

Paste someone else's high-quality code

@Override
public void Onsensorchanged (Sensorevent event) {

int sensortype = Event.sensor.getType ();
VALUES[0]:X shaft, values[1]:y shaft, VALUES[2]:Z shaft
Float[] values = event.values;
if (SensorType = = Sensor.type_accelerometer)
{
if (Math.Abs (values[0) > 17 | | Math.Abs (Values[1]) > 17 | | Math
. ABS (Values[2]) > 17))
{

/* Primary Response event */
Drawable drawable = null;
int[] IMGs = Getimgs ();
drawable = changeimg (IMGs);
Imageview.setimagedrawable (drawable);

/* Response Event End */
Shake the phone, and then accompanied by vibration tips ~ ~
Vibrator.vibrate (500);
}

}
}

Development of a small app for Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.