sunwei07

          Android入門 新手學(xué)習(xí) 開發(fā)

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            4 Posts :: 1 Stories :: 0 Comments :: 0 Trackbacks

          2011年2月15日 #

          1.在raw目錄放一個(gè)mp3文件:test.mp3;

          2.建一個(gè)MediaPlay的Service文件MusicService.java

          public class MusicService extends Service
          {
           //MediaPlayer對(duì)象
           private MediaPlayer player;

           public IBinder onBind(Intent arg0)
           {
            return null;
           }

           public void onStart(Intent intent, int startId)
           {
            super.onStart(intent, startId);
            //這里可以理解為裝載音樂文件
            player = MediaPlayer.create(this, R.raw.test);
            //開始播放
            player.start();
           }

           public void onDestroy()
           {
            super.onDestroy();
            //停止音樂-停止Service
            player.stop();
           }

          }

           3.主文件

          public void onCreate(Bundle savedInstanceState)
           {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            //從main.xml布局中獲得Button對(duì)象
            Button button_start = (Button)findViewById(R.id.start);
            Button button_stop = (Button)findViewById(R.id.stop);
            //設(shè)置按鈕(Button)監(jiān)聽
            button_start.setOnClickListener(start);
                  button_stop.setOnClickListener(stop);

           }
           
           //開始按鈕
           private OnClickListener start = new OnClickListener()
              {
                  public void onClick(View v)
                  {  
                   //開啟Service
                      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"));      
                  }
              };


          posted @ 2011-02-15 11:27 sunwei_07 閱讀(699) | 評(píng)論 (0)編輯 收藏

          public void onCreate(Bundle savedInstanceState)
           {
            TextView tv = new TextView(this);
            String string = "";  
            super.onCreate(savedInstanceState); 
            //得到ContentResolver對(duì)象
                  ContentResolver cr = getContentResolver(); 
                  //取得電話本中開始一項(xiàng)的光標(biāo)
                  Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
                  //向下移動(dòng)一下光標(biāo)
                  while(cursor.moveToNext())
                  {
                   //取得聯(lián)系人名字
                   int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);    
                   String contact = cursor.getString(nameFieldColumnIndex);
                   //取得電話號(hào)碼
                   int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);   
                   String number = cursor.getString(numberFieldColumnIndex);
                   
                   string += (contact+":"+number+"\n");
                  }
                  cursor.close();
            //設(shè)置TextView顯示的內(nèi)容
            tv.setText(string);
            //顯示到屏幕
            setContentView(tv);
           }

          posted @ 2011-02-15 11:26 sunwei_07 閱讀(358) | 評(píng)論 (0)編輯 收藏

          首先在layout里建2個(gè)xml文件

          分別有按鈕1和按鈕2

          JAVA代碼:

          1.public class Activity01 extends Activity
          {
           public void onCreate(Bundle savedInstanceState)
           {
            super.onCreate(savedInstanceState);
            /* 設(shè)置顯示main.xml布局 */
            setContentView(R.layout.main);
            /* findViewById(R.id.button1)取得布局main.xml中的button1 */
            Button button = (Button) findViewById(R.id.button1);
            /* 監(jiān)聽button的事件信息 */
            button.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v)
             {
              /* 新建一個(gè)Intent對(duì)象 */
              Intent intent = new Intent();
              /* 指定intent要啟動(dòng)的類 */
              intent.setClass(Activity01.this, Activity02.class);
              /* 啟動(dòng)一個(gè)新的Activity */
              startActivity(intent);
              /* 關(guān)閉當(dāng)前的Activity */
              Activity01.this.finish();
             }
            });
           }
          }

           

          2.public class Activity02 extends Activity
          {
           public void onCreate(Bundle savedInstanceState)
           {
            super.onCreate(savedInstanceState);
            /* 設(shè)置顯示main2.xml布局 */
            setContentView(R.layout.main2);
            /* findViewById(R.id.button2)取得布局main.xml中的button2 */
            Button button = (Button) findViewById(R.id.button2);
            /* 監(jiān)聽button的事件信息 */
            button.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v)
             {
              /* 新建一個(gè)Intent對(duì)象 */
              Intent intent = new Intent();
              /* 指定intent要啟動(dòng)的類 */
              intent.setClass(Activity02.this, Activity01.class);
              /* 啟動(dòng)一個(gè)新的Activity */
              startActivity(intent);
              /* 關(guān)閉當(dāng)前的Activity */
              Activity02.this.finish();
             }
            });
           }
          }

           


          posted @ 2011-02-15 11:25 sunwei_07 閱讀(693) | 評(píng)論 (0)編輯 收藏

          1.string.xml

          <?xml version="1.0" encoding="utf-8"?>
          <resources>
          <string name="hello">Hello World, ActivityMain!</string>
          <string name="app_name">ActivityMain</string>
          <string name="name">賬號(hào):</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);
          //由ID獲得對(duì)象
          myTextViewName=(TextView)findViewById(R.id.myTextViewName);
          myTextViewPass=(TextView)findViewById(R.id.myTextViewPass);
          //getBaseContext獲得基礎(chǔ)Context,getResources獲得資源
          Resources myColor=getBaseContext().getResources();
            //由資源myColor獲得Drawable,R.color.color3是顏色值id的引用
          Drawable color_N=myColor.getDrawable(R.color.color3);
          Drawable color_P=myColor.getDrawable(R.color.color3);
          //設(shè)置背景
          myTextViewName.setBackgroundDrawable(color_N);
          myTextViewPass.setBackgroundDrawable(color_P);

          }
          }

          posted @ 2011-02-15 11:24 sunwei_07 閱讀(348) | 評(píng)論 (0)編輯 收藏

          首先我們是在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">歡迎來(lái)到博客</string>  

           </resources>  

          而加載"歡迎來(lái)到博客"是在main.xml (定義手機(jī)布局界面的)里加入的,如下面代碼,其中我們閨將@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>  

          這樣我們運(yùn)行HelloAndroid.java時(shí),手機(jī)畫面里將顯示"歡迎來(lái)到博客"的歡迎界面,貌似我們又是沒有寫代碼,只是在.xml加了一兩行搞定,對(duì)習(xí)慣了編程的同學(xué),感覺有點(diǎn)不適應(yīng).其實(shí)在HelloAndroid.java寫代碼也可以完全達(dá)到一樣的效果.

          在這里我們首先將main.xml回歸到原樣在原樣的基礎(chǔ)上加上一行見下方(黑體行)這里ID是為了在Java類里,找到TextView對(duì)象,并且可以控制它:

          <?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,此時(shí)myTextView:text為hello            

          setContentView(R.layout.main);                        //使用findViewById函數(shù),利用ID找到該TextView對(duì)象           

           myTextView = (TextView)findViewById(R.id.myTextView);             

          String welcome_mes = "歡迎來(lái)到博客";                   //利用setText方法將TextView文字改變?yōu)閣elcom_mes           

           myTextView.setText(welcome_mes);        

           }   

           }    


          posted @ 2011-02-15 10:02 sunwei_07 閱讀(346) | 評(píng)論 (0)編輯 收藏

          僅列出標(biāo)題  
          主站蜘蛛池模板: 杭锦后旗| 鄂伦春自治旗| 察隅县| 自治县| 贞丰县| 吉安市| 清远市| 大同县| 商河县| 建始县| 罗平县| 弥勒县| 大关县| 北碚区| 邓州市| 长寿区| 永福县| 芦溪县| 冀州市| 广河县| 东兰县| 浦城县| 镇宁| 聂拉木县| 浪卡子县| 安吉县| 克什克腾旗| 都兰县| 尼玛县| 吴江市| 三江| 诸城市| 石家庄市| 竹溪县| 三原县| 奈曼旗| 长海县| 旌德县| 屏南县| 闽清县| 九龙坡区|