现在的位置: 首页 > 综合 > 正文

我的第一个Android程序helloword及个人理解

2018年02月05日 ⁄ 综合 ⁄ 共 1318字 ⁄ 字号 评论关闭

 第一次接触Android,自己写的代码,光搭建环境用了一天,先是在myeclipse9.0中,发现连接不了myeclipse官网,安装不了adt,郁闷,这天朝,连学习的网站也封

开始用代理,发现能上网站,但是更新不了插件,没办法,换成eclipse ,这东西绿色的,挺不错,第一个程序,鼓励一下吧

 

HelloWord代码:

 

public class HelloWorld extends Activity
{
    /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  //设置该Activity显示main.xml文件定义的View,及要使用的布局管理器
  setContentView(R.layout.main);
//R代表gen文件夹下系统自动生成的R.java用户不可以修改,保存的是所有的id
  Button bn = (Button)findViewById(R.id.ok);
  bn.setOnClickListener(new OnClickListener(){ //添加单击事件,单击按钮后文本变成指定的
   public void  onClick(View v)   
   {
    final TextView show = (TextView)findViewById(R.id.show);
    show.setText("Hello Android~" + new java.util.Date());
   }
  });
 }
}

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 android:id="@+id/show"
                    android:layout_width="fill_parent"
                     android:layout_height="wrap_content" 组件的高度为文字的高度
                      android:text="" />
  <Button android:id="@+id/ok"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="单击我" />
  </LinearLayout>

定义文本和按钮,id分别为show和ok

这样,单击ok后文本出现helloword +现在的时间

另外,在开发中,必须在String。xml中配置所需的字符串,而不能在Activity中直接setText(‘单击我’) 这是不允许的

必须setText(R.id.具体id);

 

抱歉!评论已关闭.