我的第一个Android程序

来源:互联网 发布:手机记事本软件 编辑:程序博客网 时间:2024/05/17 21:48
 Android也许大家还很陌生,他是Google推出的所谓的gPhone,其实他只是一个手机软件开发平台而已,最近在网络上讨论的很火的一种新技术,最近仔细研究了一下和J2ME有点类似,具体细节还需进一步研究;
我是一个对新事物充满兴趣的人,终于按耐不住,开始写了第一个黄金例程:helloWord
主程序代码如下:
package favory.hello;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.*;

public class HelloAndroid extends Activity {
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle icicle) {
        
super.onCreate(icicle);
        setContentView(R.layout.main);
        Button btn 
= (Button)HelloAndroid.this.findViewById(R.id.go);
        btn.setOnClickListener(
new View.OnClickListener() {
            
public void onClick(View v) {
                EditText edt 
= (EditText) HelloAndroid.this
                        .findViewById(R.id.edt);

                TextView txt 
= (TextView) HelloAndroid.this
                        .findViewById(R.id.txt);
                txt.setText(getString(R.string.msg_dialog) 
+ edt.getText());
            }

        }
);
    }


    
public boolean onCreateOptionsMenu(Menu menu) {
        
super.onCreateOptionsMenu(menu);
        menu.add(
0,1,R.string.menu_1);
        menu.add(
02, R.string.menu_3);
        menu.add(
03, R.string.menu_2);        
        
return true;
    }

    
    
public boolean onOptionsItemSelected(Item item){
        
super.onOptionsItemSelected(item);
        
int id=item.getId();
        
switch(id){
        
case 1:
            AlertDialog.show(
this,getString(R.string.app_name), 
                    getString(R.string.msg_dialog), getString(R.string.ok_dialog), 
true);
            
break;
        
case 2:
            AlertDialog.show(
this,getString(R.string.app_name), 
                    getString(R.string.zuozhe), getString(R.string.ok_dialog), 
true);
            
break;        
        
case 3:
            finish();
            
break;
        }

        
return true;
    }


}

资源文件夹:
res/layout/main.xml   文件内容:

<?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 id="@+id/txt"  
    android:layout_width
="fill_parent" 
    android:layout_height
="wrap_content" 
    android:text
="Hello Android,第一个google手机平台软件程序! favory.peng"
    
/>
 
<EditText id="@+id/edt"  
    android:layout_width
="fill_parent" 
    android:layout_height
="wrap_content" 
    android:text
="text">
    
<requestFocus/>
 
</EditText>   
<Button id="@+id/go"
        android:layout_width
="wrap_content" 
        android:layout_height
="wrap_content" 
        android:text
="@string/go"/>
</LinearLayout>

res/values/strings.xml  文件内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
<string name="app_name">第一个程序</string>
    
<string name="tit_dialog">提示</string>
    
<string name="msg_dialog">第一个google手机平台软件程序! favory.peng!</string>
    
<string name="ok_dialog">确定</string>
    
<string name="go">浏览</string> 
    
<string name="menu_1">信息</string> 
    
<string name="menu_2">退出</string> 
    
<string name="menu_3">作者</string> 
    
<string name="zuozhe">程序设计:彭学周   设计日期:2007-12-12</string> 
</resources>

在eclipse中编译运行结果如下:

可以点击图标执行程序;

运行现实结果

打开弹出菜单功能

显示MessageBox ,很炫吧!哈哈!

原创粉丝点击