Android入门:基本控件(一)

概述
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);
        }
}




你可能感兴趣的:(android,的)