UI的基本外形和控制1控件和基本事件的响应2键盘事件的响应3运动事件的处理4屏幕间的跳转和事件的传递5菜单的使用6弹出对话框7样式的设置1控件和基本事件的响应对于Android的应用程序,UI元素(通常称之为控件,在Android中称之为View)是其构建UI的基础。相关的两个要点:如何得到xml中的控件的句柄如何设置控件的行为控制程序的3种实现方式publicclassTestEvent1extendsActivity{privatestaticfinalStringTAG=TestEvent1;publicTestEvent1(){}@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.testevent);finalTextViewText=(TextView)findViewById(R.id.text1);finalButtonButton1=(Button)findViewById(R.id.button1);finalButtonButton2=(Button)findViewById(R.id.button2);Button1.setOnClickListener(newOnClickListener(){publicvoidonClick(Viewv){Text.setBackgroundColor(Color.RED);}});Button2.setOnClickListener(newOnClickListener(){publicvoidonClick(Viewv){Text.setBackgroundColor(Color.GREEN);}});}}publicclassTestEvent2extendsActivityimplementsOnClickListener{privatestaticfinalStringTAG=TestEvent2;privateTextViewmText;privateButtonmButton1;privateButtonmButton2;publicTestEvent2(){}@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.testevent);mText=(TextView)findViewById(R.id.text1);mButton1=(Button)findViewById(R.id.button1);mButton1.setOnClickListener(this);//设置监听的类mButton2=(Button)findViewById(R.id.button2);mButton2.setOnClickListener(this);//设置监听的类}publicvoidonClick(Viewv){Log.v(TAG,onClick());switch(v.getId()){//区分不同的控件caseR.id.button1:mText.setBackgroundColor(Color.RED);break;caseR.id.button2:mText.setBackgroundColor(Color.GREEN);break;default:break;}}}publicclassTestEvent3extendsActivity{privatestaticfinalStringTAG=TestEvent3;privateTextViewmText;privateButton1_OnClickListenermListener1=newButton1_OnClickListener();privateButton2_OnClickListenermListener2=newButton2_OnClickListener();publicTestEvent3(){}classButton1_OnClickListenerimplementsOnClickListener{publicvoidonClick(Viewv){mText.setBackgroundColor(Color.RED);}}classButton2_OnClickListenerimplementsOnClickListener{publicvoidonClick(Viewv){mText.setBackgroundColor(Color.GREEN);}}@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.testevent);mText=(TextView)findViewById(R.id.text1);finalButtonmButton1=(Button)findViewById(R.id.button1);finalButtonmButton2=(Button)findViewById(R.id.button2);mButton1.setOnClickListener(mListener1);//设置监听者的类mButton2.setOnClickListener(mListener2);//设置监听者的类}}要点:使用findViewById获取XML中UI元素的句柄使用setOnXXXListener()设置事件处理函数setOnClickListener是类android.view.View类的函数,每一个UI元素都继承的这个类,同族的函数包括:voidsetOnClickListener(View.OnClickListenerl);voidsetOnCreateContextMenuListener(View.OnCreateContextMenuListenerl);voidsetOnFocusChangeListener(View.OnFocusChangeListenerl);voidsetOnKeyListener(View.OnKeyListenerl);voidsetOnLongClickListener(View.OnLongClickListenerl);voidsetOnTouchListener(View.OnTouchListenerl);2键盘事件的响应在应用的程序的控制方面,更多的使用的是屏幕上的控件,但是有的时候也需要直接对键盘事件来进行响应。键盘是Android中主要的输入设备,对按键的响应的处理是响应之间在程序中使用键盘的核心内容。按键信息:按键码、按键的动作(抬起、按下)、重复信息、扫描码码。使用上键和右键增加图片的Alpha值,使用下键和左键减少图片的Alpha值LinearLayoutxmlns:android=:id=@+id/screenandroid:layout_width=fill_parentandroid:layout_height=fill_parentandroid:orientation=verticalTextViewandroid:id=@+id/alphavalueandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_gravity=center/ImageViewandroid:id=@+id/imageandroid:src=@drawable/robotandroid:layout_gravity=centerandroid:layout_width=wrap_contentandroid:layout_height=wrap_content//LinearLayout布局文件:packagecom.android.basicapp;importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.*;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.KeyEvent;importandroid.view.View;importandroid.widget.TextView;importandroid.widget.ImageView;publicclassTestKeyEventextendsActivity{privatestaticfinalStringTAG=TestKeyEvent;privateImageViewmImage;privateTextViewmAlphavalueText;privateintmAlphavalue;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.testkeyevent);mImage=(ImageView)findViewById(R.id.image);mAlphavalueText=(TextView)findViewById(R.id.alphavalue);mAlphavalue=100;mImage.setAlpha(mAlphavalue);mAlphavalueText.setText(Alpha=+mAlphavalue*100/0xff+%);}代码:@OverridepublicbooleanonKeyDown(intkeyCode,KeyEventmsg){Log.v(TAG,onKeyDown:keyCode=+keyCode);Log.v(TAG,onKeyDown:String=+msg.toString());switch(keyCode){caseKeyEvent.KEYCODE_DPAD_UP:caseKeyEvent.KEYCODE_DPAD_RIGHT:mAlphavalue+=20;break;caseKeyEvent.KEYCODE_DPAD_DOWN:caseKeyEvent.KEYCODE_DPAD_LEFT:mAlphavalue-=20;break;default:break;}if(mAlphavalue=0xFF)mAlphavalue=0xFF;if(mAlphavalue=0x0)mAlphavalue=0x0;mImage.setAlpha(mAlphavalue);mAlphavalueText.setText(Alpha=+mAlphavalue*100/0xff+%);returnsuper.onKeyDown(keyCode,msg);}}finalintgetAction()//获得按键的动作finalintgetFlags()//获得标志finalintgetKeyCode()//获得按键码finalintgetRepeatCount()//获得重复的信息finalintgetScanCode()//获得扫描码onKeyDown()函数来获得按键的事件,同类的函数还包括onKeyUp()函数,其参数intkeyCode为按键码,KeyEventmsg表示按