`

android 成功第一个程序

 
阅读更多

          今天是2012年11月24日,这是我在程序中最开心的日子,我终于做了一个小程序,并且放在自己手机上成功安装运行了。哦也!

           非常简单

             1 一个xml

             1 一个java文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

 <Button android:id="@+id/btnShowDate" android:layout_width="wrap_content"
     android:layout_height="wrap_content" android:text="显示当前日期"
 />
 <Button android:id="@+id/btnShowTime" android:layout_width="wrap_content"
     android:layout_height="wrap_content" android:text="显示当天日期"
     />
 <Button android:id="@+id/btnShowName" android:layout_width="wrap_content"
     android:layout_height="wrap_content" android:text="显示姓名"
     />
</LinearLayout>

 

 

package com.example.ch02_showdatetime;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

 @Override
 public void onClick(View v) {
  switch(v.getId())// TODO Auto-generated method stub
  {
    case R.id.btnShowDate:
    {
     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
     showDialog("当前日期",sdf.format(new Date()));
     break;
    }
    case R.id.btnShowTime:
    {
     SimpleDateFormat sdf=new SimpleDateFormat("HH-mm-ss");
     showDialog("当前时间",sdf.format(new Date()));
     break;
    }
    case R.id.btnShowName:
    {
     
     showDialog("hello word","钟艳明第一个android 程序");
     break;
    }
  }
  
 }

 private void showDialog(String title, String message) {
  AlertDialog.Builder builder=new AlertDialog.Builder(this);
  builder.setIcon(android.R.drawable.ic_dialog_info);
  builder.setTitle(title);
  builder.setMessage(message);
  builder.setPositiveButton("确定", null);
  builder.create().show();
  
 }
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Button btnShowDate=(Button)findViewById(R.id.btnShowDate);
  Button btnShowTime=(Button)findViewById(R.id.btnShowTime);
  Button btnShowName=(Button)findViewById(R.id.btnShowName);
  btnShowDate.setOnClickListener(this);
  btnShowTime.setOnClickListener(this);
  btnShowName.setOnClickListener(this);
 }
 
 

}

 

然后将生成的Apk 复制到手机上就运行了,哦也!嘿嘿!

 

Apk 需要签名,个人推介JDK 的krytool 工具,因为很多版本不兼容!

努力,android 加油!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics