锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 1.鍦╮aw鐩綍鏀句竴涓猰p3鏂囦歡錛歵est.mp3; 2.寤轟竴涓狹ediaPlay鐨凷ervice鏂囦歡MusicService.java public class MusicService extends Service public IBinder onBind(Intent arg0) public void onStart(Intent intent, int startId) public void onDestroy() } 3.涓繪枃浠?/p>
public void onCreate(Bundle savedInstanceState) }
{
//MediaPlayer瀵硅薄
private MediaPlayer player;
{
return null;
}
{
super.onStart(intent, startId);
//榪欓噷鍙互鐞嗚В涓鴻杞介煶涔愭枃浠?br />
player = MediaPlayer.create(this, R.raw.test);
//寮濮嬫挱鏀?br />
player.start();
}
{
super.onDestroy();
//鍋滄闊充箰-鍋滄Service
player.stop();
}
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//浠巑ain.xml甯冨眬涓幏寰桞utton瀵硅薄
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
//璁劇疆鎸夐挳錛圔utton錛夌洃鍚?br />
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
//寮濮嬫寜閽?br />
private OnClickListener start = new OnClickListener()
{
public void onClick(View v)
{
//寮鍚疭ervice
startService(new Intent("com.yarin.Android.MUSIC"));
}
};
//鍋滄鎸夐挳
private OnClickListener stop = new OnClickListener()
{
public void onClick(View v)
{
//鍋滄Service
stopService(new Intent("com.yarin.Android.MUSIC"));
}
};
]]>
{
TextView tv = new TextView(this);
String string = "";
super.onCreate(savedInstanceState);
//寰楀埌ContentResolver瀵硅薄
ContentResolver cr = getContentResolver();
//鍙栧緱鐢佃瘽鏈腑寮濮嬩竴欏圭殑鍏夋爣
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//鍚戜笅縐誨姩涓涓嬪厜鏍?br />
while(cursor.moveToNext())
{
//鍙栧緱鑱旂郴浜哄悕瀛?br />
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//鍙栧緱鐢佃瘽鍙風爜
int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
String number = cursor.getString(numberFieldColumnIndex);
string += (contact+":"+number+"\n");
}
cursor.close();
//璁劇疆TextView鏄劇ず鐨勫唴瀹?br />
tv.setText(string);
//鏄劇ず鍒板睆騫?br />
setContentView(tv);
}
棣栧厛鍦╨ayout閲屽緩2涓獂ml鏂囦歡
鍒嗗埆鏈夋寜閽?鍜屾寜閽?
JAVA浠g爜錛?/p>
1.public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 璁劇疆鏄劇ずmain.xml甯冨眬 */
setContentView(R.layout.main);
/* findViewById(R.id.button1)鍙栧緱甯冨眬main.xml涓殑button1 */
Button button = (Button) findViewById(R.id.button1);
/* 鐩戝惉button鐨勪簨浠朵俊鎭?*/
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 鏂板緩涓涓狪ntent瀵硅薄 */
Intent intent = new Intent();
/* 鎸囧畾intent瑕佸惎鍔ㄧ殑綾?*/
intent.setClass(Activity01.this, Activity02.class);
/* 鍚姩涓涓柊鐨凙ctivity */
startActivity(intent);
/* 鍏抽棴褰撳墠鐨凙ctivity */
Activity01.this.finish();
}
});
}
}
2.public class Activity02 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 璁劇疆鏄劇ずmain2.xml甯冨眬 */
setContentView(R.layout.main2);
/* findViewById(R.id.button2)鍙栧緱甯冨眬main.xml涓殑button2 */
Button button = (Button) findViewById(R.id.button2);
/* 鐩戝惉button鐨勪簨浠朵俊鎭?*/
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 鏂板緩涓涓狪ntent瀵硅薄 */
Intent intent = new Intent();
/* 鎸囧畾intent瑕佸惎鍔ㄧ殑綾?*/
intent.setClass(Activity02.this, Activity01.class);
/* 鍚姩涓涓柊鐨凙ctivity */
startActivity(intent);
/* 鍏抽棴褰撳墠鐨凙ctivity */
Activity02.this.finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ActivityMain!</string>
<string name="app_name">ActivityMain</string>
<string name="name">璐﹀彿:</string>
<string name="pass">瀵嗙爜:</string>
</resources>
2鑷畾涔夌殑.drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="color1">#ffffff</color>
<color name="color2">#938192</color>
<color name="color3">#7cd12e</color>
</resources>
3.main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/color1">
<TextView
android:id="@+id/myTextViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="@color/color2"
android:layout_x="61px"
android:layout_y="69px"
/>
<TextView
android:id="@+id/myTextViewPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pass"
android:textColor="@color/color2"
android:layout_x="61px"
android:layout_y="158px"
/>
<EditText
android:id="@+id/myEditTextName"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="114px"
android:layout_y="57px"
/>
<EditText
android:id="@+id/myEditTextPass"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:textSize="18sp"
android:password="true"
android:layout_x="112px"
android:layout_y="142px"
/>
</AbsoluteLayout>
4.ActivityMain.java
package org.Gofe.drawable;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;
public class ActivityMain extends Activity {
/** Called when the activity is first created. */
private TextView myTextViewName;
private TextView myTextViewPass;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//鐢盜D鑾峰緱瀵硅薄
myTextViewName=(TextView)findViewById(R.id.myTextViewName);
myTextViewPass=(TextView)findViewById(R.id.myTextViewPass);
//getBaseContext鑾峰緱鍩虹Context,getResources鑾峰緱璧勬簮
Resources myColor=getBaseContext().getResources();
//鐢辮祫婧恗yColor鑾峰緱Drawable,R.color.color3鏄鑹插糹d鐨勫紩鐢?/span>
Drawable color_N=myColor.getDrawable(R.color.color3);
Drawable color_P=myColor.getDrawable(R.color.color3);
//璁劇疆鑳屾櫙
myTextViewName.setBackgroundDrawable(color_N);
myTextViewPass.setBackgroundDrawable(color_P);
}
}
棣栧厛鎴戜滑鏄湪res->values->string.xml閲岄潰鍔犱簡濡備笅涓鍙?榛戜綋):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid</string>
<string name="app_name">HelloAndroid</string>
<string name="textView_text">嬈㈣繋鏉ュ埌鍗氬</string>
</resources>
鑰屽姞杞?嬈㈣繋鏉ュ埌鍗氬"鏄湪main.xml (瀹氫箟鎵嬫満甯冨眬鐣岄潰鐨?閲屽姞鍏ョ殑,濡備笅闈唬鐮?鍏朵腑鎴戜滑闂哄皢@string/hello 鏀規垚浜咢string/textView_text .
<?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="fill_parent" android:layout_height="wrap_content" android:text="@string/textView_text" /> </LinearLayout>
榪欐牱鎴戜滑榪愯HelloAndroid.java鏃?鎵嬫満鐢婚潰閲屽皢鏄劇ず"嬈㈣繋鏉ュ埌鍗氬"鐨勬榪庣晫闈?璨屼技鎴戜滑鍙堟槸娌℃湁鍐欎唬鐮?鍙槸鍦?xml鍔犱簡涓涓よ鎼炲畾,瀵逛範鎯簡緙栫▼鐨勫悓瀛?鎰熻鏈夌偣涓嶉傚簲.鍏跺疄鍦℉elloAndroid.java鍐欎唬鐮佷篃鍙互瀹屽叏杈懼埌涓鏍風殑鏁堟灉.
鍦ㄨ繖閲屾垜浠鍏堝皢main.xml鍥炲綊鍒板師鏍峰湪鍘熸牱鐨勫熀紜涓婂姞涓婁竴琛岃涓嬫柟(榛戜綋琛?榪欓噷ID鏄負浜嗗湪Java綾婚噷,鎵懼埌TextView瀵硅薄,騫朵笖鍙互鎺у埗瀹?
<?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:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>
鍦ㄤ富紼嬪簭HelloAndroid.java閲屼唬鐮佸涓?
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
private TextView myTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //杞藉叆main.xml Layout,姝ゆ椂myTextView:text涓篽ello
setContentView(R.layout.main); //浣跨敤findViewById鍑芥暟,鍒╃敤ID鎵懼埌璇extView瀵硅薄
myTextView = (TextView)findViewById(R.id.myTextView);
String welcome_mes = "嬈㈣繋鏉ュ埌鍗氬"; //鍒╃敤setText鏂規硶灝員extView鏂囧瓧鏀瑰彉涓簑elcom_mes
myTextView.setText(welcome_mes);
}
}