WebView的基本使用
//直接加载网页
webView.loadUrl("http://www.baidu.com");
//final String mimeType = "text/html";
//webView.loadData("<a href='http://www.baidu.com'>Hello World!</a>", mimeType, null);
webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient()
{
//获取进度
@Override
public void onProgressChanged(WebView view, int newProgress)
{
pb.setProgress(newProgress);
}
//获取网页标题
@Override
public void onReceivedTitle(WebView view, String title)
{
super.onReceivedTitle(view, title);
tv.setText(title);
}
});
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
{
//download file
}
});
//回退键
webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction()==KeyEvent.ACTION_DOWN){
if(keyCode==KeyEvent.KEYCODE_BACK&&webView.canGoBack()){
webView.goBack();
return true;
}
}
return false;
}
});
//支持JavaScript
webView.setJavaScriptEnabled(true);
TextSwicher的基本使用
mTs = (TextSwitcher)findViewById(R.id.ts);
mTs.setFactory(new ViewSwitcher.ViewFactory()
{
@Override
public View makeView()
{
TextView tv = new TextView(Marquee.this);
tv.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
tv.setTextSize(36);
return tv;
}
});
//淡入淡出动画
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
TextClock使用
显示日期、时间
//默认格式
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
//只显示日期
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="MMM dd, yyyy"
android:format24Hour="MMM dd, yyyy"/>
//只显示时间
<TextClock
android:format12Hour="hh:mm:ss a"
android:format24Hour="HH:mm:ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//设置timeZone
<TextClock
android:format12Hour="'Time in Paris:' MMM dd, yyyy h:mma"
android:format24Hour="'Time in Paris:' MMM dd, yyyy HH:mm"
android:timeZone="Europe/Paris"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//显示星期
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="EE"
android:format24Hour="EE"/>
Switcher
Android自带的开关控件
//默认选中
<Switch
android:checked="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip"
android:text="Switch with match_parent width"/>
//自定义文本
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip"
android:text="custom text"
android:textOff="textOff"
android:textOn="textOn"/>
//通过setOnCheckedChangeListener(this);设置监听,开关状态改变时进行相应的操作
<Switch
android:id="@+id/monitored_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip"
android:text="Monitored switch"/>
//多文本
<Switch
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip"
android:gravity="top|left"
android:singleLine="false"
android:text="This is an example of a switch with a lot of text in it. It may end up wrapping to another line. The switch will be pinned at the top."/>
<Switch
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip"
android:gravity="center_vertical|center_horizontal"
android:singleLine="false"
android:text="This is an example of a switch with a lot of text in it. It may end up wrapping to another line. The switch will be vertically centered."/>
<Switch
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_marginBottom="32dip"
android:gravity="bottom|right"
android:singleLine="false"
android:text="This is an example of a switch with a lot of text in it. It may end up wrapping to another line. The switch will be pinned at the bottom."/>