CSDN简单安卓客户端实现

一直是在电脑上面看CSDN的博客、资讯,今天做了个小软件在手机上面查看资讯和博客。

                    

直接上代码:

一、首先是欢迎界面 --  LoadingActivity.java

package com.example.webviewtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class LoadingActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loading);
		
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				
				Intent mainIntent = new Intent(LoadingActivity.this,
						ShowActivity.class);
				
				startActivity(mainIntent);
				finish();
			}
		}, 2000);
	}
	
}
其布局文件很简单--loading.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/splash" />

</LinearLayout>

二、主页面--ShowActivity.java
package com.example.webviewtest;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class ShowActivity extends Activity {

	private WebView webView;
	private static Boolean isExit = false;
	Timer tExit = new Timer();// 计时器
	TimerTask task;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		webView = (WebView) findViewById(R.id.web_view);

		webView.getSettings().setBuiltInZoomControls(true);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setRenderPriority(RenderPriority.HIGH);
		// webView.getSettings().setBlockNetworkImage(true);

		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// 根据传入的参数再去加载新的网页
				view.loadUrl(url);
				// 表示当前WebView可以处理打开新网页的请求,不用借助系统浏览器
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);

				Toast.makeText(ShowActivity.this, "呃!加载失败,也许断网了,或者不能访问!",
						Toast.LENGTH_LONG).show();
			}
		});

		webView.loadUrl("http://www.csdn.net/");
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
			webView.goBack();// 返回前一个页面
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onBackPressed() {
		if (isExit == false) {
			isExit = true;
			Toast.makeText(this, "再按一次返回键回到桌面", Toast.LENGTH_SHORT).show();
			task = new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			};
			tExit.schedule(task, 2000);
		} else {
			finish();
			System.exit(0);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		super.onCreateOptionsMenu(menu);
		// 添加四个菜单项

		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "帮助").setIcon(
				R.drawable.menu_help);

		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "退出").setIcon(
				R.drawable.menu_quit);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {

		case Menu.FIRST + 1:
			Intent intent = new Intent(ShowActivity.this, HelpActivity.class);
			startActivity(intent);
			return true;

		case Menu.FIRST + 2:
			new AlertDialog.Builder(this)
					.setTitle("退出")
					.setMessage("你确定要退出CSDN吗?")
					.setIcon(android.R.drawable.ic_dialog_info)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									finish();// 退出程序
								}
							}).setNegativeButton(R.string.cancel, null).show();
			return true;
		}
		return false;
	}

}
布局文件--activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

三、帮助页面--HelpActivity.java

package com.example.webviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class HelpActivity extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.help);
	}

	
}
布局文件--help.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1,2" >

    <TableRow>

        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:text="第一步:"
            android:textSize="23sp" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"
            android:text="第二步:"
            android:textSize="23sp" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:text="第三步:"
            android:textSize="23sp" />
    </TableRow>

    <TableRow
        android:layout_marginTop="20dp">

       <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:text="右上角图标"
            android:textSize="15sp" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"
            android:text="点击博客"
            android:textSize="15sp" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:text="点击登录"
            android:textSize="15sp" />
    </TableRow>

    <TableRow>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginRight="10dp"
            android:src="@drawable/first" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:src="@drawable/second" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:src="@drawable/third" />
    </TableRow>

</TableLayout>
四、最后别忘了在AndroidManifest.xml文件中添加访问网络的权限

 <uses-permission android:name="android.permission.INTERNET" />


五、Apk下载地址:http://download.csdn.net/detail/xdwyyan/8121301



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值