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/imageView1" 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"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:text="第一步:" android:textSize="23sp" /> <TextView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:text="第二步:" android:textSize="23sp" /> <TextView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:text="第三步:" android:textSize="23sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <TextView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:text="点击右上角图标" android:textSize="15sp" /> <TextView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:text="点击博客" android:textSize="15sp" /> <TextView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:text="点击登录" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginRight="10dp" android:src="@drawable/first" /> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:src="@drawable/second" /> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:src="@drawable/third" /> </LinearLayout> </LinearLayout>四、最后别忘了在AndroidManifest.xml文件中添加访问网络的权限
<uses-permission android:name="android.permission.INTERNET" />
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。