Android常用控件(一)

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."/>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值