1. WebView
<uses-permission android:name="android.permission.INTERNET" />
WebView wv = (WebView) findViewById(R.id.web_view);
//支持 JavaScript 脚本
wv.getSettings().setJavaScriptEnabled(true);
//防止跳转到系统浏览器
wv.setWebViewClient(new WebViewClient());
//加载指定网址
wv.loadUrl("http://www.baidu.com");
清理Cookie
android.webkit.CookieManager.getInstance().removeAllCookie()
2. ScrollView
- 能包含一个子控件,若想添加多个控件,可添加一个布局
问题解决:自动显示到中间位置;
修改父布局属性
android:focusable="true" android:focusableInTouchMode="true"
滚动到底部或者顶部:
scrollView.fullScroll( ScrollView.FOCUS_DOWN)
当我们动态添加View时候,调用上边这个方法不能跳转到底部。因为Android 很多函数都是基于消息队列来同步,当addView() 之后,View不会马上显示出来,虽然时间很短,但是马上调用 fullScroll( ) view 可能还没有显示出来,所以会没有效果。 解决方法:
scrollView.post { scrollView.fullScroll(ScrollView.FOCUS_DOWN) }
3. DataPickerDialog
//日期选择器
final Calendar calendar = Calendar.getInstance(); //当前日期
DatePickerDialog dialog = new DatePickerDialog(DatePickerDialogActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
calendarChoose.set(year,month,day);
String mDay=Integer.toString(day);
if(day<10){ //格式化
mDay="0"+mDay;
}
tv.setText(year + "-" + (month + 1) + "-" + mDay);
} //下方这几个值为日期选择器初始值
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
注意: month 默认是从0开始算起
设置最小日期选择值:dia.getDatePicker().setMinDate(new Date().getTime());
4. TimePickerDialog
//时间选择器
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
String mHour=Integer.toString(hourOfDay) ;
String mMinute=Integer.toString(minute) ;
if(hourOfDay<10){
mHour="0"+hourOfDay;
}
if(minute<10){
mMinute="0"+minute;
}
tv.setText(mHour+":"+mMinute);
}
},0,0,true); //设置初始值 为 00:00
timePickerDialog.show();
5. ViewFlipper
简单使用: 直接添加ImageView
flipInterval
自动切换时间,
<ViewFlipper
android:id="@+id/viewFilpper"
android:flipInterval="2000"
android:layout_centerHorizontal="true"
android:layout_below="@+id/viewPager"
android:layout_width="200dp"
android:layout_height="200dp">
<ImageView
android:src="@drawable/img1"
android:layout_width="200dp"
android:layout_height="200dp" />
<ImageView
android:src="@drawable/img2"
android:layout_width="200dp"
android:layout_height="200dp" />
<ImageView
android:src="@drawable/img3"
android:layout_width="200dp"
android:layout_height="200dp" />
</ViewFlipper>
ViewFlipper viewFlipper=(ViewFlipper)findViewById(R.id.viewFilpper);
viewFlipper.startFlipping(); //开始
6. ImageView
src属性和background属性的区别
src填入图片时,是按照图片的大小直接填充,并不会进行拉伸。
而是用background填入图片时,则会根据ImageView给定的宽度来进行拉伸。
7.Spinner
下拉菜单
<Spinner
android:id="@+id/activity_main_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
设置Adapter
String[] datas={"Item 1","Item 2","Item 3","Item 4","Item 5"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,datas);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
关于 setDropDownViewResource 设置下拉列表项的布局样式
代码设置选中项:
spinner.setSelection(2);
选中事件:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
spinner.getSelectedItem().toString(); //选中项文本
spinner.getSelectedItemId(); //选中项文本
spinner.getSelectedItemPosition(); //选中项位置,从1开始
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
常用属性:
completionThreshoid | 说明 |
---|---|
overlapAnchor (没有智能提示) | 弹出位置是否在Spinner下方 |
dropDownVerticalOffset | 水平方向偏移量 |
8.AutoCompleteTextView
常用属性:
completionThreshoid | 指定用户输入多少个字符后才会显示提示 |
---|---|
completionHint | 设置下拉菜单的提示标题 |
dropDownHeight | 设置下拉菜单的高度 |
dropDownWidth | 设置下拉菜单的宽度 |
popuBackground | 设置下拉菜单的背景 |
设置Adapter:
String[] datas={"123","12","11111","1111222","2223333"};
AutoCompleteTextView acTv=(AutoCompleteTextView)findViewById(R.id.activity_test_auto_tv);
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,datas);
acTv.setAdapter(adapter);
9.SeekBar
常用属性:
max | 滑动条的最大值 |
---|---|
progress | 滑动条的当前值 |
thumb | 滑块的drawable |
事件:
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//进度发生改变时触发
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvSeekBarInfo.setText("当前进度值 "+seekBar.getProgress()+"/"+seekBar.getMax());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { //按住SeekBar触发
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) { //松开SeekBar触发
}
});
10.ProgressDialog
progressDialog = new ProgressDialog(mContext);
progressDialog.setCancelable(true); //设置是否可以通过点击Back取消
progressDialog.setCanceledOnTouchOutside(true);//点击Dialog外区域取消显示Dialog进度条
progressDialog.setMessage(""); //设置显示的内容
progressDialog.setTitle(""); //设置标题
progressDialog.setIcon(R.mipmap.ic_launcher); //Tittle的图标,如果设置Title的话Icon是不会显示的
//ProgressDialog.STYLE_HORIZONTAL 水平进度条
//ProgressDialog.STYLE_SPINNER 圆形进度条
progressDialog.setProgressStyle(STYLE_HORIZONTAL); //设置进度条显示样式
progressDialog.setMax(30); //这是进度条最大值
progressDialog.setProgress(1); //设置进度条的进度值
//监听 Dismiss
progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
}
});
//设置可点击的按钮,默认情况下有三个可选值
//DialogInterface.BUTTON_POSITIVE //确定
//DialogInterface.BUTTON_NEGATIVE //取消
//DialogInterface.BUTTON_NEUTRAL //中立
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
progressDialog.dismiss();
}
});
11.RadioButton
若果须要实现单选功能,需要报RadioButton放到RadioGroup中,通过RadioGroup的orientation属性来设置排列方向
获取选中RadioButton的两种方式:
RadioGroup rg=(RadioGroup)findViewById(R.id.activity_test_rg); //选中后获取
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rbtn=(RadioButton)findViewById(checkedId);
Toast.makeText(TestActivity.this, rbtn.getText(), Toast.LENGTH_SHORT).show();
}
});
//手动获取(也就是需要点击Button,必须有默认选中
RadioGroup rg=(RadioGroup)findViewById(R.id.activity_test_rg);
RadioButton rbtn=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
Toast.makeText(TestActivity.this, rbtn.getText(), Toast.LENGTH_SHORT).show();
12.VideoView
- 获取播放位置:
videoView.getCurrentPosition();
- 指定播放位置开始播放:
videoView.seekTo(position); vidoeView.start();
13.RatingBar
星级评分条
属性 | 描述 |
---|---|
numStars | 显示多少颗星星 |
rating | 默认评分值 |
stepSize | 评分每次增加的值 |
14. PopupMenu
轻量化,弹出式菜单
// Show Popup Menu
void showPopupMenu(){
// 1.为View指定 PopupMenu
PopupMenu pm=new PopupMenu(MenuActivity.this,btnPopupMenu);
// 2.指定 Menu 资源文件
pm.getMenuInflater().inflate(R.menu.menu_test,pm.getMenu());
//Item 点击事件
pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_item1:
toast("Menu1");
break;
case R.id.menu_item2:
toast("Menu2");
break;
}
return true;
}
});
pm.show(); //3. 显示
}