概述 android的界面编程和swimg有很多类似的地方,几乎android的控件都在swimg出现过,在单个activity里编程一般只要修改源文件和main.xml。解释直接写在代码里。 1 Button main.xml <? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical" >

< TextView
android:id ="@+id/MyTextView"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content" />
< Button
android:id ="@+id/MyButton"
android:text ="点击产生随机数"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content" />


</ LinearLayout >
ButtonActivity package me.chendd.button;

import java.util.Random;

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

public class AndroidButtonActivity extends Activity {
/** Called when the activity is first created. */
private TextView MyTextview = null;
private Button MyButton = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyTextview = (TextView)findViewById(R.id.MyTextView);
MyButton = (Button)findViewById(R.id.MyButton);
// 添加监听器
MyButton.setOnClickListener( new ButtonListener());
}
// 内部类实现
class ButtonListener implements OnClickListener {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 产生随机数
String str = new Random().nextInt()+"";
MyTextview.setText(str);
}

}
}

2 ImageButton
main.xml <? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical" >

< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content" />
< ImageButton
android:id ="@+id/MyImageButton"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="点击切换图片" />

</ LinearLayout >
ImageButton package me.chendd.imagebutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class AndroidImageButtonActivity extends Activity {
/** Called when the activity is first created. */
private ImageButton MyImageButton = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyImageButton = (ImageButton) findViewById(R.id.MyImageButton);
MyImageButton.setOnTouchListener( new OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// 注意此处v和MyImageButton都可以
MyImageButton.setBackgroundResource(R.drawable.ic_launcher);
} else if(event.getAction() == MotionEvent.ACTION_UP) {
MyImageButton.setBackgroundResource(R.drawable.icon);
}
return true;
}
});
}
}
3 EditText main.xml
<? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical" >

< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/hello" />
<!-- android:inputType="number":输入类型为数字; -->
<!-- android:maxLength="2":输入最长为2; -->
<!-- android:password="true" :输入的形式为密码 -->
<!-- android:numeric="integer":输入整数 -->
< EditText
android:id ="@+id/et"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:singleLine ="true"
android:text ="@string/hello" />

</ LinearLayout >
EditTextActivity package me.chendd.edittext;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class AndroidEditTextActivity extends Activity {
/** Called when the activity is first created. */
private EditText et = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.et);
}
}



更多相关文章

  1. [Android]静态广播监听器
  2. android之【事件监听器】
  3. Android Random随机数
  4. Android笔记Android基于事件监听器处理机制
  5. Android:为控件绑定监听器
  6. Java,Android Integer和byte的相互转换,Java Android给定范围随机
  7. Android笔记二十三.Android基于事件监听器处理机制
  8. Android:Random生成随机数
  9. Android疑惑记录-----在Adapter中配置按钮监听器时,列表项内容获

随机推荐

  1. Android最新开源框架大全
  2. Android基础知识大纲
  3. android 向web服务器发送post请求并获取
  4. Android(安卓)Fastboot
  5. Android实现透明式状态栏
  6. Android的布局控件----LinearLayout(线性
  7. SDK Platform Tools component is missin
  8. android TabHost小结
  9. 实现ListView的item逐个飞入效果——Layo
  10. android camera(一):camera模组CMM介绍