android 之控件篇
来源:互联网 发布:java orm 编辑:程序博客网 时间:2024/06/08 10:19
1.Menu:菜单,点击菜单键后弹出的选项菜单
private boolean isMore = false;// menu菜单翻页控制AlertDialog menuDialog;// menu菜单DialogGridView menuGrid;View menuView;private final int ITEM_SEARCH = 0;// 搜索private final int ITEM_FILE_MANAGER = 1;// 文件管理private final int ITEM_DOWN_MANAGER = 2;// 下载管理private final int ITEM_FULLSCREEN = 3;// 全屏private final int ITEM_MORE = 11;// 菜单/** 菜单图片 **/int[] menu_image_array = { R.drawable.menu_search, R.drawable.menu_filemanager, R.drawable.menu_downmanager,R.drawable.menu_fullscreen, R.drawable.menu_inputurl, R.drawable.menu_bookmark,R.drawable.menu_bookmark_sync_import, R.drawable.menu_sharepage, R.drawable.menu_quit,R.drawable.menu_nightmode, R.drawable.menu_refresh, R.drawable.menu_more };/** 菜单文字 **/String[] menu_name_array = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签", "加入书签", "分享页面", "退出", "夜间模式", "刷新", "更多" };/** 菜单图片2 **/int[] menu_image_array2 = { R.drawable.menu_auto_landscape, R.drawable.menu_penselectmodel,R.drawable.menu_page_attr, R.drawable.menu_novel_mode, R.drawable.menu_page_updown,R.drawable.menu_checkupdate, R.drawable.menu_checknet, R.drawable.menu_refreshtimer,R.drawable.menu_syssettings, R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return };/** 菜单文字2 **/String[] menu_name_array2 = { "自动横屏", "笔选模式", "阅读模式", "浏览模式", "快捷翻页", "检查更新", "检查网络", "定时刷新", "设置", "帮助", "关于","返回" };<pre class="html" name="code">@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/************复选框*********///根据ID找到该文本控件 tv = (TextView)this.findViewById(R.id.tvSex); //根据ID找到RadioGroup实例 RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup); //绑定一个匿名监听器 group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // TODO Auto-generated method stub //获取变更后的选中项的ID int radioButtonId = arg0.getCheckedRadioButtonId(); //根据ID获取RadioButton的实例 RadioButton rb = (RadioButton)MainActivity.this.findViewById(radioButtonId); //更新文本内容,以符合选中项 tv.setText("您的性别是:" + rb.getText()); } });menuView = View.inflate(this, R.layout.gridview_menu, null);// 找出main下面的gridview_menu// 创建AlertDialogmenuDialog = new AlertDialog.Builder(this).create();menuDialog.setView(menuView);menuDialog.setOnKeyListener(new OnKeyListener() {public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键dialog.dismiss();return false;}});menuGrid = (GridView) menuView.findViewById(R.id.gridview);menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));/** 监听menu选项 **/menuGrid.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {switch (arg2) {case ITEM_SEARCH:// 搜索break;case ITEM_FILE_MANAGER:// 文件管理break;case ITEM_DOWN_MANAGER:// 下载管理break;case ITEM_FULLSCREEN:// 全屏break;case ITEM_MORE:// 翻页if (isMore) {menuGrid.setAdapter(getMenuAdapter(menu_name_array2, menu_image_array2));isMore = false;} else {// 首页menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));isMore = true;}menuGrid.invalidate();// 更新menumenuGrid.setSelection(ITEM_MORE);break;}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add("menu");// 必须创建一项return super.onCreateOptionsMenu(menu);}private SimpleAdapter getMenuAdapter(String[] menuNameArray, int[] imageResourceArray) {ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();for (int i = 0; i < menuNameArray.length; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put("itemImage", imageResourceArray[i]);map.put("itemText", menuNameArray[i]);data.add(map);}SimpleAdapter simperAdapter = new SimpleAdapter(this, data, R.layout.item_menu,new String[] { "itemImage", "itemText" }, new int[] { R.id.item_image, R.id.item_text });return simperAdapter;}@Overridepublic boolean onMenuOpened(int featureId, Menu menu) {if (menuDialog == null) {menuDialog = new AlertDialog.Builder(this).setView(menuView).show();} else {menuDialog.show();}return false;// 返回为true 则显示系统menu}
2.Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。而且Toast主要用于向用户显示提示消息,当然大家也可以根据自己的需求来自定义自己想要的效果。
1.
Toast.makeText(getApplicationContext(), "默认Toast样式", Toast.LENGTH_SHORT).show();
2.
toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
3.
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();
4
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom, (ViewGroup) findViewById(R.id.llToast)); ImageView image = (ImageView) layout .findViewById(R.id.tvImageToast); image.setImageResource(R.drawable.icon); TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); title.setText("Attention"); TextView text = (TextView) layout.findViewById(R.id.tvTextToast); 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();
5.
new Thread(new Runnable() { public void run() { showToast(); } }).start();
3.Progressbar
(1) 带有进度条的 ProgressBar
MainActivity.java
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Request the progress bar to be shown in the titlerequestWindowFeature(Window.FEATURE_PROGRESS);setContentView(R.layout.activity_main);setProgressBarVisibility(true);// 设置在title里的ProgressBar可见final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);setProgress(progressHorizontal.getProgress() * 100);// 为title中的ProgressBar设置进度setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);// 为title中的ProgressBar设置二级进度Button button = (Button) findViewById(R.id.increase);// 一级进度递增button.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {progressHorizontal.incrementProgressBy(1);// Title progress is in range 0..10000setProgress(100 * progressHorizontal.getProgress());// 为title中的ProgressBar设置进度}});button = (Button) findViewById(R.id.decrease);// 一级进度递减button.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {progressHorizontal.incrementProgressBy(-1);// Title progress is in range 0..10000setProgress(100 * progressHorizontal.getProgress());// 为title中的ProgressBar设置进度}});button = (Button) findViewById(R.id.increase_secondary);// 二级进度递增button.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {progressHorizontal.incrementSecondaryProgressBy(1);// Title progress is in range 0..10000setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());}});button = (Button) findViewById(R.id.decrease_secondary);// 二级进度递减button.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {progressHorizontal.incrementSecondaryProgressBy(-1);// Title progress is in range 0..10000setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());}});}}
activity_main.xml
<ProgressBar android:id="@+id/progress_horizontal" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dip" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="75" /> <Button android:id="@+id/increase" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginTop="100dp" android:layout_marginLeft="50dp" /> <Button android:id="@+id/decrease" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginTop="100dp" android:layout_marginLeft="150dp"/> <Button android:id="@+id/increase_secondary" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginTop="200dp" android:layout_marginLeft="50dp"/> <Button android:id="@+id/decrease_secondary" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginTop="200dp" android:layout_marginLeft="150dp"/>
实现的效果
转圈样式的ProgressBar
MainActivity.java
</pre><pre class="html" name="code"> // Request for the progress bar to be shown in the title requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); // Make sure the progress bar is visible setProgressBarVisibility(true);
activity_main.xml
<!-- 大样式 --> <ProgressBar android:id="@+id/progress_large" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 默认 --> <ProgressBar android:id="@+android:id/progress" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 小样式 --> <ProgressBar android:id="@+id/progress_small" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 小标题样式 --> <ProgressBar android:id="@+id/progress_small_title" style="?android:attr/progressBarStyleSmallTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" />
实现效果
(3) 对话框样式的进度
MainActivity.java
private static final int DIALOG1_KEY = 0; private static final int DIALOG2_KEY = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.showIndeterminate); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DIALOG1_KEY); } }); button = (Button) findViewById(R.id.showIndeterminateNoTitle); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DIALOG2_KEY); } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG1_KEY: { ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle("Indeterminate"); dialog.setMessage("Please wait while loading..."); dialog.setIndeterminate(true);//设置转圈的效果 dialog.setCancelable(true); return dialog; } case DIALOG2_KEY: { ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("Please wait while loading..."); dialog.setIndeterminate(true);//设置转圈的效果 dialog.setCancelable(true); return dialog; } } return null; }
activity_main.xml
<Button android:id="@+id/showIndeterminate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/progress_large" android:layout_marginTop="72dp" android:layout_toRightOf="@+id/progress_small" android:text="Button" /> <Button android:id="@+id/showIndeterminateNoTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/showIndeterminate" android:layout_alignBottom="@+id/showIndeterminate" android:layout_alignParentRight="true" android:layout_marginRight="52dp" android:text="Button" />
效果图
0 0
- android 之控件篇
- android之自定义控件篇
- Android控件篇之视图控件scrollview探索
- Android控件之TabHost 转载篇
- Android自定义控件系列之基础篇
- Android四大控件之Activity:理论篇
- Android 四大控件之 Activity 篇
- Android 控件之对话框
- Android 控件之ListView
- Android 控件之Spinner
- Android 控件之GridView
- Android 控件之GridView
- android控件之Textview
- android控件之EditText
- Android控件之ExpandableListView
- Android控件之ExpandableListView
- android控件之SeekBar
- Android控件之ListView
- Maximo 密码加密传送
- NSFileHandle处理文件
- java操作mysql中文乱码解决
- nodejs搭建CNPM本地库管理
- Android监测程序压入后台及从后台返回
- android 之控件篇
- 面向对象的思想
- /bin/bash: prebuilts/misc/linux-x86/bison/bison: No such file or directory
- matlab 实践程序8——将指定的图像移动到另一个文件夹
- OC基础语法
- iOS8 定位问题
- jquery获取当前点击对象
- Barbican 简介
- zookeeper 安装 windows环境