Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1479605
  • 博文数量: 204
  • 博客积分: 4013
  • 博客等级: 中校
  • 技术积分: 4030
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-29 06:34
文章分类

全部博文(204)

文章存档

2012年(204)

分类: Java

2012-05-15 18:42:43

HelloAndroid.java代码

点击(此处)折叠或打开

  1. package com.studio.android;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;

  9. public class HelloAndroid extends Activity {
  10.     /** Called when the activity is first created. */
  11.    private Button btnShow,btnClear;
  12.    private EditText edtInput;
  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.           
  18.         btnShow=(Button)findViewById(R.id.btnShow);//控件与代码绑定
  19.         btnClear=(Button)findViewById(R.id.btnClear);//控件与代码绑定
  20.         edtInput=(EditText)findViewById(R.id.edtInput);//控件与代码绑定
  21.        //设置按钮监听器
  22.         btnShow.setOnClickListener(new ClickListener());//使用点击事件
  23.         btnClear.setOnClickListener(new ClickListener());//使用点击事件
  24.     }
  25.       
  26.  //接着下面写个类实现OnClickListener接口
  27.  private final class ClickListener implements OnClickListener
  28.     {
  29.         public void onClick(View v)
  30.         {
  31.             if(v==btnShow)
  32.             {
  33.                 new AlertDialog.Builder(HelloAndroid.this)
  34.                 .setIcon(android.R.drawable.ic_dialog_alert)
  35.                 .setTitle("Information")
  36.                 .setMessage(edtInput.getText())
  37.                 .show();
  38.             }
  39.             else if(v==btnClear)
  40.             {
  41.                 edtInput.setText("HelloAndroid");
  42.             }
  43.         }
  44.     }
  45. }

UI布局代码main.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7. <EditText android:text="EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edtInput"></EditText>
  8. <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center">
  9. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show" android:id="@+id/btnShow"></Button>
  10. <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Clear" android:id="@+id/btnClear"></Button>
  11. </LinearLayout>
  12. </LinearLayout>

运行图:

阅读(1548) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~