您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 薪酬管理 > Android用户界面程序设计示例
1Android用户界面程序设计示例[例1]按钮和Toast弹出对话框..............................................................................................1[例2]TextView文本框(1)................................................................................................4[例3]TextView文本框(2).................................................................................................5[例4]编辑框EditText..............................................................................................................6[例5]单选RadioButton..........................................................................................................12[例6]Toast的用法简介.........................................................................................................14[例7]多选checkbox...............................................................................................................18[例8]菜单Menu.....................................................................................................................20[例9]Dialog对话框...............................................................................................................22[例10]图片视图ImageView..................................................................................................25[例11]图片按钮ImageButton...............................................................................................27界面布局.................................................................................................................................31[例12]垂直线性布局.....................................................................................................31[例13]水平线性布局.....................................................................................................33[例14]相对布局.............................................................................................................34绝对布局.........................................................................................................................35[例15]表单布局.............................................................................................................35[例16]切换卡(TabWidget)402[例1]按钮和Toast弹出对话框1、设计界面如图所示:2、布局文件:TextViewandroid:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:text=@string/hello/Buttonandroid:id=@+id/okandroid:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:text=OK/3、Activity界面程序:publicclassActivity01extendsActivity{publicvoidonCreate(BundlesavedInstanceState){3super.onCreate(savedInstanceState);setContentView(R.layout.main);//获得Button对象Buttonbutton_ok=(Button)findViewById(R.id.ok);//设置Button控件监听器button_ok.setOnClickListener(newButton.OnClickListener(){publicvoidonClick(Viewv){//这里处理事件//DisplayToast(点击了OK按钮);Toast.makeText(this,(点击了OK按钮,Toast.LENGTH_SHORT).show();}});}publicvoidDisplayToast(Stringstr){Toast.makeText(this,str,Toast.LENGTH_SHORT).show();}/*按键按下所触发的事件*/publicbooleanonKeyDown(intkeyCode,KeyEventevent){switch(keyCode){caseKeyEvent.KEYCODE_DPAD_CENTER:DisplayToast(按下:中键);break;caseKeyEvent.KEYCODE_DPAD_UP:DisplayToast(按下:上方向键);break;caseKeyEvent.KEYCODE_DPAD_DOWN:DisplayToast(按下:下方向键);break;caseKeyEvent.KEYCODE_DPAD_LEFT:DisplayToast(按下:左方向键);break;caseKeyEvent.KEYCODE_DPAD_RIGHT:DisplayToast(按下:右方向键);break;}returnsuper.onKeyDown(keyCode,event);}/*按键弹起所触发的事件*/publicbooleanonKeyUp(intkeyCode,KeyEventevent){switch(keyCode){4caseKeyEvent.KEYCODE_DPAD_CENTER:DisplayToast(弹起:中键);break;caseKeyEvent.KEYCODE_DPAD_UP:DisplayToast(弹起:上方向键);break;caseKeyEvent.KEYCODE_DPAD_DOWN:DisplayToast(弹起:下方向键);break;caseKeyEvent.KEYCODE_DPAD_LEFT:DisplayToast(弹起:左方向键);break;caseKeyEvent.KEYCODE_DPAD_RIGHT:DisplayToast(弹起:右方向键);break;}returnsuper.onKeyUp(keyCode,event);}[例2]TextView(1)1、设计界面如图所示:2、布局文件:TextViewandroid:id=@+id/textviewandroid:layout_width=fill_parentandroid:layout_height=wrap_content5android:text=@string/hello/3、Activity界面程序的核心语句:textview=(TextView)this.findViewById(R.id.textview);Stringstring=TextView示例,wangzhiguo;/*设置文本的颜色*/textview.setTextColor(Color.RED);/*设置字体大小*/textview.setTextSize(20);/*设置文字背景*/textview.setBackgroundColor(Color.BLUE);/*设置TextView显示的文字*/textview.setText(string);[例3]TextView(2)1、设计界面(略)2、布局文件:TextViewandroid:id=@+id/textviewandroid:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:text=@string/helloandroid:background=#FFFFFFandroid:textColor=#000000android:textSize=20px/其他一些属性android:textColor=#ff0000android:textSize=24spandroid:textStyle=bold3、Activity界面程序的核心语句:setContentView(R.layout.main);//设置内容显示的xml布局文件6TextViewtextView=(TextView)findViewById(R.id.text_view);//取得TextView组件textView.setTextColor(Color.RED);//设置成红色textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,24f);//设置成24sptextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗android:autoLink=webandroid:autoLink=phoneandroid:autoLink=all实现跑马灯效果1.TextView2.android:id=@+id/text_view3.android:autoLink=all4.android:layout_width=fill_parent5.android:layout_height=wrap_content6.android:text=@string/hello7.android:ellipsize=marquee8.android:focusable=true9.and
本文标题:Android用户界面程序设计示例
链接地址:https://www.777doc.com/doc-6261653 .html