sunwei07

Android入门 新手学习 开发

BlogJava 首页 新随笔 联系 聚合 管理
  4 Posts :: 1 Stories :: 0 Comments :: 0 Trackbacks

2011年2月15日 #

1.在raw目录放一个mp3文件:test.mp3;

2.建一个MediaPlay的Service文件MusicService.java

public class MusicService extends Service
{
 //MediaPlayer对象
 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对象
  Button button_start = (Button)findViewById(R.id.start);
  Button button_stop = (Button)findViewById(R.id.stop);
  //设置按钮(Button)监听
  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 阅读(682) | 评论 (0)编辑 收藏

public void onCreate(Bundle savedInstanceState)
 {
  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);
        //向下移动一下光标
        while(cursor.moveToNext())
        {
         //取得联系人名字
         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显示的内容
  tv.setText(string);
  //显示到屏幕
  setContentView(tv);
 }

posted @ 2011-02-15 11:26 sunwei_07 阅读(344) | 评论 (0)编辑 收藏

首先在layout里建2个xml文件

分别有按钮1和按钮2

JAVA代码:

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)
   {
    /* 新建一个Intent对象 */
    Intent intent = new Intent();
    /* 指定intent要启动的类 */
    intent.setClass(Activity01.this, Activity02.class);
    /* 启动一个新的Activity */
    startActivity(intent);
    /* 关闭当前的Activity */
    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)
   {
    /* 新建一个Intent对象 */
    Intent intent = new Intent();
    /* 指定intent要启动的类 */
    intent.setClass(Activity02.this, Activity01.class);
    /* 启动一个新的Activity */
    startActivity(intent);
    /* 关闭当前的Activity */
    Activity02.this.finish();
   }
  });
 }
}

 


posted @ 2011-02-15 11:25 sunwei_07 阅读(676) | 评论 (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">账号:</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获得对象
myTextViewName=(TextView)findViewById(R.id.myTextViewName);
myTextViewPass=(TextView)findViewById(R.id.myTextViewPass);
//getBaseContext获得基础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);
//设置背景
myTextViewName.setBackgroundDrawable(color_N);
myTextViewPass.setBackgroundDrawable(color_P);

}
}

posted @ 2011-02-15 11:24 sunwei_07 阅读(336) | 评论 (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">欢迎来到博客</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加了一两行搞定,对习惯了编程的同学,感觉有点不适应.其实在HelloAndroid.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为hello            

setContentView(R.layout.main);                        //使用findViewById函数,利用ID找到该TextView对象           

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

String welcome_mes = "欢迎来到博客";                   //利用setText方法将TextView文字改变为welcom_mes           

 myTextView.setText(welcome_mes);        

 }   

 }    


posted @ 2011-02-15 10:02 sunwei_07 阅读(330) | 评论 (0)编辑 收藏

仅列出标题