android应用面试宝典;footerview自动添加在下翻页的最后一项;主界面返回键退出提示
全局类
package com.kane.interviewcollection.util;
import com.kane.interviewcollection.dbc.SqliteConnection;
import android.app.Activity;
public class Globals {
public static int SCREEN_WIDTH;
public static int SCERRN_HEIGHT;
public static SqliteConnection dbc;
public static void init(Activity a){
dbc=new SqliteConnection(a);//对应需要的activity类
SCERRN_HEIGHT=a.getWindowManager().getDefaultDisplay().getHeight();
SCREEN_WIDTH=a.getWindowManager().getDefaultDisplay().getWidth();
}
}
baseactivity类,其他activity继承该类,除了封面activity
package com.kane.interviewcollection.util;
import com.kane.interviewcollection.MainActivity;
import com.kane.interviewcollection.QuesstionActivity;
import com.kane.interviewcollection.R;
import com.kane.interviewcollection.SearchActivity;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
/**
* 防止调用这个进入界面,所以定义成抽象类
*
* @author lenovo
*
*/
public abstract class BaseActivity extends Activity {
TextView titleText;
private Button settingBtn;
// 下面的三个按钮
private Button[] bottomBtns = new Button[3];
// 准备目标的Activity,因为下面的按钮跳转是公共的,写在这个BaseActivity
private Class[] targetActivities = { MainActivity.class,
QuesstionActivity.class, SearchActivity.class };
// 未选中的按钮图片
private int[] allBottomImgs = { R.drawable.bottom_icon01_a,
R.drawable.bottom_icon02_a, R.drawable.bottom_icon03_a };
// 选中的按钮图片
private int[] allBottomImgsSelected = { R.drawable.bottom_icon01_b,
R.drawable.bottom_icon02_b, R.drawable.bottom_icon03_b };
// 声明循环为了在内部类使用
private int i;
// 点击设置按钮的浮动窗口
private PopupWindow win;
/**
* 这里不需要界面
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* 在子类activity中调用的初始化方法
*
* @param title
* @param selectedBtn
*/
public void init(String title, int selectedBtn) {
// 分别取得公共界面的组件
settingBtn = (Button) findViewById(R.id.settingBtn);
titleText = (TextView) findViewById(R.id.title);
bottomBtns[0] = (Button) findViewById(R.id.bottom01);
bottomBtns[1] = (Button) findViewById(R.id.bottom02);
bottomBtns[2] = (Button) findViewById(R.id.bottom03);
// 每个界面的标题不一样
titleText.setText(title);
// 循环为底部的按钮加入监听,这里的i内部类要用,在外面声明了
for (i = 0; i < bottomBtns.length; i++) {
// 定义个临时变量,防止i一直自增,因为循环完一边后i已经是2了,第二次循环一开始内部类中就是3了
final int temp = i;
if (i == selectedBtn) {
// 选中的
bottomBtns[i].setBackgroundResource(allBottomImgsSelected[i]);
} else {
// 没有选中的
bottomBtns[i].setBackgroundResource(allBottomImgs[i]);
// 加入监听
bottomBtns[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 加入切换界面操作
Intent in = new Intent(BaseActivity.this,
targetActivities[temp]);
startActivity(in);
}
});
}
}
/**
* 设置按钮的监听事件
*/
settingBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 没有浮动框要添加
if (win == null) {
// 先设置宽高,再设置界面
win = new PopupWindow(Globals.SCREEN_WIDTH / 4,
Globals.SCERRN_HEIGHT / 6);
View view = LayoutInflater.from(BaseActivity.this).inflate(
R.layout.float_window, null);
win.setContentView(view);
// 加入这些内部按钮的监听
TextView versionText = (TextView) v
.findViewById(R.id.show_version);
TextView authText = (TextView) v
.findViewById(R.id.about_author);
TextView exitText = (TextView) v.findViewById(R.id.exit);
versionText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BaseActivity.this, "当前版本是: 1.0",
Toast.LENGTH_LONG).show();
win.dismiss();// 关闭窗口
}
});
authText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Builder builder = new Builder(BaseActivity.this);
builder.setTitle("提示");
builder.setMessage("本作品由needkane发布,欢迎使用");
builder.setNegativeButton("关闭",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
});
builder.create().show();
win.dismiss();// 浮动窗口消失
}
});
exitText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 退出程序
}
});
}
if (win.isShowing()) {
// 隐藏
win.dismiss();
} else {
// 显示
win.showAsDropDown(settingBtn);
}
}
});
}
}
进来主页
package com.kane.interviewcollection;
import com.kane.interviewcollection.util.BaseActivity;
import com.kane.interviewcollection.util.DialogManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.init("程序员面试宝典", 0);//第一个底部button点击
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
new DialogManager().showExitHit(this);
return false;//执行上面,而不是默认的关闭activity
}
return super.onKeyDown(keyCode, event);//其他键原有功能保存
}
}
问题列表类
package com.kane.interviewcollection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.kane.interviewcollection.util.BaseActivity;
import com.kane.interviewcollection.util.QuesstionDAOUtils;
public class QuesstionActivity extends BaseActivity {
private ListView listView;
private SimpleAdapter adapter;
private List<Map<String, Object>> allValues=new ArrayList<Map<String,Object>>();
//声明几个分页的变量
private int pageNo=1;
private int pageSize=20;
private int count=0;
private int allPages=0;
private String keyword="";
//为了调用滑动事件中的内部类的另一个方法的参数
private int first;
private int visCount;
private int total;
private TextView footerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quesstion);
//接收关键字
keyword=getIntent().getStringExtra("keyword");
if (keyword==null) {
super.init("全部问题",1);
keyword="";
}
else{
//如果有关键词传入
super.init("搜索:"+keyword,1);
}
listView=(ListView)findViewById(R.id.list);
allValues=QuesstionDAOUtils.listDataPage(pageNo,pageSize,keyword);
//查询记录数
count=QuesstionDAOUtils.getAllCount(keyword);
//计算总页数
allPages=(count-1)/pageSize+1;
//加入一个提示的底部信息,每次下拉到分页的最后一个item
footerText=new TextView(this);
footerText.setText("z正在查询,请稍后.............");
footerText.setTextColor(Color.BLACK);
footerText.setTextSize(14);
listView.addFooterView(footerText);
//把数据加入到adapter里
adapter=new SimpleAdapter(this,allValues,R.layout.question_line
,new String[] {"question"},new int[]{R.id.line_question});
listView.setAdapter(adapter);
//加入监听,点击后才可以看到答案
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//取得数据
Map<String,Object> map=allValues.get(arg2);
LinearLayout line =(LinearLayout)arg1;//arg1是当前行界面
if(line.getChildCount()==1){//里面只有问题那一个textview
//加入答案的textview
TextView text = new TextView(QuesstionActivity.this);
text.setText(map.get("answer").toString());
text.setTextSize(14);
text.setTextColor(Color.RED);
line.addView(text);
}
else{
//删除答案提示
line.removeViewAt(1);
}
}
});
/**
* 滑动列表事件
*/
listView.setOnScrollListener(new OnScrollListener(){
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
first = firstVisibleItem;
visCount = visibleItemCount;
total = totalItemCount;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState==OnScrollListener.SCROLL_STATE_IDLE&&
first+visCount==total&&total!=0){
if(pageNo<allPages){
//可以查询下一条数据
pageNo++;
//将新查询的数据合并到原有的数据集合
allValues.addAll(QuesstionDAOUtils.listDataPage(pageNo,
pageSize,keyword));
//通知界面修改
adapter.notifyDataSetChanged();
}
else{
// 已经查询了全部的数据,将footer删除
if (listView.getFooterViewsCount() > 0) {
listView.removeFooterView(footerText);
}
}
}
}});
}
}
搜索问题类
package com.kane.interviewcollection;
import com.kane.interviewcollection.util.BaseActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SearchActivity extends BaseActivity {
private Button searchBtn;
private EditText searchText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
super.init("搜索问题",2);
searchBtn=(Button) findViewById(R.id.searchBtn);
searchText=(EditText)findViewById(R.id.searchText);
searchBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text=searchText.getText().toString();
Intent in=new Intent(SearchActivity.this,QuesstionActivity.class);
in.putExtra("keyword",text);
startActivity(in);
}
});
}
}
添加对话框,返回键退出
package com.kane.interviewcollection.util;
import com.kane.interviewcollection.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
public class DialogManager {
public void showExitHit(final Activity act) {
final AlertDialog alertDialog = new AlertDialog.Builder(act)
.setTitle(R.string.exitHint)
.setMessage(R.string.exitChoose)
.setPositiveButton(R.string.sure,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
ExitApplication.getInstance().exit();
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
return;
}
}).create();//先创建,才能show
alertDialog.show();
}
}
package com.kane.interviewcollection.util;
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class ExitApplication extends Application {
private List<Activity> activityList = new LinkedList<Activity>();
private static ExitApplication instance;
private ExitApplication() {
}
// 单例模式中获取唯一的ExitApplication实例
public static ExitApplication getInstance() {
if (null == instance) {
instance = new ExitApplication();
}
return instance;
}
// 添加Activity到容器中
public void addActivity(Activity activity) {
activityList.add(activity);
}
// 遍历所有Activity并finish
public void exit() {
for (Activity activity : activityList) {
activity.finish();//先得全部遍历关掉
}
System.exit(0);
}
}
头部界面
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/top_bg"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingRight="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_vertical"
android:text="程序员面试宝典"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/settingBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/top_setting" />
</LinearLayout>
尾部界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.2"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.7"
android:background="@drawable/bottom_toolbar"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<Button
android:id="@+id/bottom01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bottom_icon01_b" />
<Button
android:id="@+id/bottom02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bottom_icon02_a" />
<Button
android:id="@+id/bottom03"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bottom_icon03_a" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
</LinearLayout>
问题列表界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/home_bg"
android:orientation="vertical" >
<!-- 提出公共界面 -->
<include layout="@layout/topbar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/middle_content_bg"
android:cacheColorHint="#00000000"
android:divider="#cccccc"
android:dividerHeight="1dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:scrollbars="none" >
<!-- android:divider="#cccccc"
android:dividerHeight="1dp"设置分割线 -->
</ListView>
</LinearLayout>
<include layout="@layout/bottombar" />
</LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。