6-Android用户界面菜单
- 格式:ppt
- 大小:2.13 MB
- 文档页数:38
Android用户界面程序设计示例[例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)40[例1]按钮和Toast弹出对话框1、设计界面如图所示:2、布局文件:<TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><Buttonandroid:id="@+id/ok"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="OK"/>3、Activity界面程序:public class Activity01 extends Activity {public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);// 获得Button对象Button button_ok = (Button) findViewById(R.id.ok);// 设置Button控件监听器button_ok.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {// 这里处理事件//DisplayToast("点击了OK按钮");Toast.makeText(this, ("点击了OK按钮", Toast.LENGTH_SHORT).show();}});}public void DisplayToast(String str) {Toast.makeText(this, str, Toast.LENGTH_SHORT).show();}/* 按键按下所触发的事件*/public boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_DPAD_CENTER:DisplayToast("按下:中键");break;case KeyEvent.KEYCODE_DPAD_UP:DisplayToast("按下:上方向键");break;case KeyEvent.KEYCODE_DPAD_DOWN:DisplayToast("按下:下方向键");break;case KeyEvent.KEYCODE_DPAD_LEFT:DisplayToast("按下:左方向键");break;case KeyEvent.KEYCODE_DPAD_RIGHT:DisplayToast("按下:右方向键");break;}return super.onKeyDown(keyCode, event);}/* 按键弹起所触发的事件*/public boolean onKeyUp(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_DPAD_CENTER:DisplayToast("弹起:中键");break;case KeyEvent.KEYCODE_DPAD_UP:DisplayToast("弹起:上方向键");break;case KeyEvent.KEYCODE_DPAD_DOWN:DisplayToast("弹起:下方向键");break;case KeyEvent.KEYCODE_DPAD_LEFT:DisplayToast("弹起:左方向键");break;case KeyEvent.KEYCODE_DPAD_RIGHT:DisplayToast("弹起:右方向键");break;}return super.onKeyUp(keyCode, event);}[例2]TextView(1)1、设计界面如图所示:2、布局文件:<TextViewandroid:id="@+id/textview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/>3、Activity界面程序的核心语句:textview = (TextView)this.findViewById(R.id.textview);String string = "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/textview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"android:background="#FFFFFF"android:textColor="#000000"android:textSize="20px"/>其他一些属性android:textColor="#ff0000"android:textSize="24sp"android:textStyle="bold"3、Activity界面程序的核心语句:setContentView(yout.main);//设置内容显示的xml布局文件TextView textView=(TextView)findViewById(R.id.text_view);//取得TextView组件textView.setTextColor(Color.RED);//设置成红色textView.setTextSize(PLEX_UNIT_SP, 24f);//设置成24sp textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗android:autoLink="web"android:autoLink="phone"android:autoLink="all"实现跑马灯效果<TextViewandroid:id="@+id/text_view"1.android:autoLink="all"2.android:layout_width="fill_parent"3.android:layout_height="wrap_content"4.android:text="@string/hello"5.android:ellipsize="marquee"6.android:focusable="true"7.android:marqueeRepeatLimit="marquee_forever"8.android:focusableInTouchMode="true"9.android:singleLine="true"10. android:scrollHorizontally="true"/>11.</LinearLayout>[例4]编辑框EditText1、设计界面如图所示:2、布局文件:<string name="hello">文本框中内容是</string><string name="message">请输入账号</string><string name="app_name">EditText_wangzhiguo</string><TextViewandroid:id="@+id/TextView01"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><EditTextandroid:id="@+id/EditText01"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="18sp"android:layout_x="29px"android:hint="@string/message"android:layout_y="33px"/>3、Activity界面程序的核心语句:super.onCreate(savedInstanceState);setContentView(yout.main);m_TextView = (TextView) findViewById(R.id.TextView01);m_EditText = (EditText) findViewById(R.id.EditText01);m_TextView.setTextSize(20);/*** 设置当m_EditText中为空时提示的内容在XML中同样可以实现:android:hint="请输入账号"*/// m_EditText.setHint("请输入账号");/* 设置EditText事件监听 */m_EditText.setOnKeyListener(new EditText.OnKeyListener() { @Overridepublic boolean onKey(View arg0, int arg1, KeyEvent arg2) { // 得到文字,将其显示到TextView中m_TextView.setText(Activity01.this.getString(R.string.hello) +m_EditText.getText().toString());return false;}});补充:关于EditText的一些细节操作android:hint="请输入用户名..." 提示属性android:textColorHint="#238745" 更改提示颜色android:enabled="false" 不可编辑android:lines=”10”通过设定行高,实现文本域功能android:maxLength="40" 最大内容长度android:password="true" 要求输入密码android:phoneNumber="true" 只能输入电话号码droid:numeric="signed"android:inputType="date" 指定输入类型android:imeOptions="actionSearch" Enter键图标设置1.actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:2.actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:3.actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:4.actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果:5.actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:6.actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:7.actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:课堂练习作业提示//监听EditText文本的回车键editText.setOnEditorActionListener(new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();return false;}});//获取EditText文本public void onClick(View v) {Toast.makeText(HelloEditText.this, editText.getText() .toString(), Toast.LENGTH_SHORT).show();Button all=(Button)findViewById(R.id.btn_all);all.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {editText.selectAll();}});//让EditText全选Button all=(Button)findViewById(R.id.btn_all);all.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {editText.selectAll();}});//从第2个字符开始选择EditText文本public void onClick(View v) {Editable editable=editText.getText();Selection.setSelection(editable, 1,editable.length());}public void onClick(View v) {int start=editText.getSelectionStart();int end=editText.getSelectionEnd();CharSequence selectText=editText.getText().subSequence(start, end);oast.makeText(HelloEditText.this, selectText, Toast.LENGTH_SHORT) .show();}/*** 交换两个变量的值* @param start 变量初值* @param end 变量终值*/protected void switchIndex(int start, int end) {int temp=start;start=end;end=temp;}[例5]单选RadioButton1、设计界面如图所示:2、布局文件:<resources><string name="hello">Android底层是基于什么操作系统?</string> <string name="app_name">单选RadioButton_wangzhiguo</string> <string name="RadioButton1">Windows</string><string name="RadioButton2">Linux</string><string name="RadioButton3">Moc os</string><string name="RadioButton4">Java</string></resources><TextViewandroid:id="@+id/TextView01"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><RadioGroupandroid:id="@+id/RadioGroup01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:layout_x="3px"android:layout_y="54px"><RadioButtonandroid:id="@+id/RadioButton1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/RadioButton1"/><RadioButtonandroid:id="@+id/RadioButton2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/RadioButton2"/><RadioButtonandroid:id="@+id/RadioButton3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/RadioButton3"/><RadioButtonandroid:id="@+id/RadioButton4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/RadioButton4"/></RadioGroup>3、Activity界面程序的核心语句:/*** 获得TextView对象获得RadioGroup对象获得4个RadioButton对象*/m_TextView = (TextView) findViewById(R.id.TextView01);m_RadioGroup = (RadioGroup) findViewById(R.id.RadioGroup01);m_Radio1 = (RadioButton) findViewById(R.id.RadioButton1);m_Radio2 = (RadioButton) findViewById(R.id.RadioButton2);m_Radio3 = (RadioButton) findViewById(R.id.RadioButton3);m_Radio4 = (RadioButton) findViewById(R.id.RadioButton4);/* 设置事件监听 */m_RadioGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stubif (checkedId == m_Radio2.getId()) {DisplayToast("正确答案:" + m_Radio2.getText()+ ",恭喜你,回答正确!");} else {DisplayToast("请注意,回答错误!");}}});}/* 显示Toast */public void DisplayToast(String str) {Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);// 设置toast显示的位置toast.setGravity(Gravity.TOP, 0, 220);// 显示该Toasttoast.show();}[例6]Toast的用法简介[例6_1] 弹出式提示框的默认样式1、设计界面如图所示:2、核心语句:Toast.makeText(getApplicationContext(), "默认Toast样式", Toast.LENGTH_SHORT).show();[例6_2] 自定义提示框显示位置1、设计界面如图所示:2、核心语句:toast = Toast.makeText(getApplicationContext(),"自定义位置Toast", Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);toast.show();[例6_3]带图片提示框效果1、设计界面如图所示:2、核心语句:toast = Toast.makeText(getApplicationContext(),"带图片的Toast", Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);LinearLayout toastView = (LinearLayout) toast.getView();ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon);toastView.addView(imageCodeProject, 0);toast.show();[例6_4]带图片的自定义提示框效果1、设计界面如图所示:2、核心语句:LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(yout.custom,(ViewGroup) findViewById(R.id.llToast));ImageView image = (ImageView) layout.findViewById(ImageToast);image.setImageResource(R.drawable.icon);TextView title = (TextView) layout.findViewById(TitleToast); title.setText("Attention");TextView text = (TextView) layout.findViewById(TextToast); text.setText("完全自定义Toast");toast = new Toast(getApplicationContext());toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);toast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);toast.show();[例6_5]其他线程1、设计界面如图所示:2、核心语句:new Thread(new Runnable() {public void run() {showToast();}}).start();[例7]多选checkbox1、设计界面如图所示:2、布局文件:<string name="hello">调查:你喜欢Android的原因?</string><string name="app_name">CheckBox_wangzhiguo</string><string name="CheckBox1">无界限的应用程序</string><string name="CheckBox2">应用程序是在平等的条件下创建的</string> <string name="CheckBox3">应用程序可以轻松地嵌入网络</string><string name="CheckBox4">应用程序可以并行运行</string><TextViewandroid:id="@+id/TextView1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><CheckBoxandroid:id="@+id/CheckBox1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/CheckBox1"></CheckBox><CheckBoxandroid:id="@+id/CheckBox4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/CheckBox4"></CheckBox><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="提交"></Button>3、核心语句:m_CheckBox1.setOnCheckedChangeListener(newCheckBox.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (m_CheckBox1.isChecked()) {DisplayToast("你选择了:" + m_CheckBox1.getText());}}});m_Button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) {int num = 0;if (m_CheckBox1.isChecked()) {num++;}if (m_CheckBox2.isChecked()) {num++;}if (m_CheckBox3.isChecked()) {num++;}if (m_CheckBox4.isChecked()) {num++;}DisplayToast("谢谢参与!你一共选择了" + num + "项!");}});[例8] 菜单Menu1、设计界面如图所示:2、布局文件:<string name="hello">主界面,点击关于会跳到另一个界面!(Activity01)</string> <string name="hello2">关于\nAndroid Menu使用范例!(Activity02)</string> <string name="app_name">Menu_wangzhiguo</string><string name="ok">切换Activity</string><string name="back">返回</string>创建menu文件夹,其中放入menu.xml<menu xmlns:android="/apk/res/android"> <item android:id="@+id/about"android:title="关于"/><item android:id="@+id/exit"android:title="退出"/></menu>创建两个main.xml,两个activity,并且在AndroidManifest.xml中加入<activity android:name=".Activity02" ></activity>3、Activity界面程序的核心语句:启用菜单/* 创建menu */public boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();// 设置menu界面为res/menu/menu.xmlinflater.inflate(R.menu.menu, menu);return true;}/* 处理菜单事件 */public boolean onOptionsItemSelected(MenuItem item) {// 得到当前选中的MenuItem的ID,int item_id = item.getItemId();switch (item_id) {case R.id.about:/* 新建一个Intent对象 */Intent intent = new Intent();/* 指定intent要启动的类 */intent.setClass(Activity01.this, Activity02.class);/* 启动一个新的Activity */startActivity(intent);/* 关闭当前的Activity */Activity01.this.finish();break;case R.id.exit:Activity01.this.finish();break;}return true;}启用菜单的另外一种方式public boolean onCreateOptionsMenu(Menu menu) { // 为menu添加内容menu.add(0, 0, 0, R.string.ok);menu.add(0, 1, 1, R.string.back);return true;}[例9] Dialog对话框1、设计界面如图所示:2、核心语句:Dialog dialog = new AlertDialog.Builder(this).setTitle("exit").setMessage("你确定退出程序吗").setNegativeButton("取消", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) // Acitivity01.this.finish();Acitivity01.this.loginDialog().show();}}).setPositiveButton("ok", newDialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {pDialog= ProgressDialog.show(Acitivity01.this, "请稍等", "您正在登陆", true);new Thread(){public void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}pDialog.dismiss();};}.start();Acitivity01.this.finish();}}).create();dialog.show();public Dialog loginDialog(){L ayoutInflater factory = LayoutInflater.from(Acitivity01.this);V iew dialogView = factory.inflate(yout.dialog, null);D ialog dialog = null;A lertDialog.Builder builder = newAlertDialog.Builder(Acitivity01.this);b uilder.setTitle("this is a login view");b uilder.setView(dialogView);b uilder.setPositiveButton("ok", null);b uilder.setNegativeButton("cancel", null);d ialog = builder.create();r eturn dialog;}[例10] 图片视图ImageView1、设计界面如图所示:2、布局文件:<ImageViewandroid:id="@+id/ImageView01"android:layout_width="wrap_content"android:layout_height="wrap_content"></ImageView><TextViewandroid:id="@+id/TextView01"android:layout_below="@id/ImageView01"android:layout_width="wrap_content"android:layout_height="wrap_content">3、核心语句:// 获得ImageView的对象imageview = (ImageView) this.findViewById(R.id.ImageView01);textview = (TextView) this.findViewById(R.id.TextView01);// 设置imageview的图片资源。