Android九宫格界面实现点击每个格点击跳转界面

时间:2023-03-10 03:16:46
Android九宫格界面实现点击每个格点击跳转界面

刚开始有个任务就是做一个九宫格界面,后来有个任务就是实现点击每个格并跳转界面实现每个格的功能。下面我就介绍一下我是如何实现该功能的

首先写一下我的想法是:

登录成功后显示一个九宫格界面,每个九宫格的每一个都是一个功能模块,当点击每个模块时,就会跳转到相应的界面并实现该模块所具备的功能。

下面是以"综合实践管理系统"这个格来实现的,当我们点击该按钮时,他就会跳转到"学生综合实践模块积分申请表"这个界面然后我们通过下拉菜单选择自己想要申请的项目。然后点击"下一步"我们就跳转到补充信息这个界面了,实现上传照片还有补充信息这个功能界面。

Android九宫格界面实现点击每个格点击跳转界面

第二步:跳转到下面这个界面

Android九宫格界面实现点击每个格点击跳转界面

第三步点击下一步跳转到完善信息这个界面

Android九宫格界面实现点击每个格点击跳转界面

点击加号上传你要加分的证明图片,这个照片可以调用相机照也可以是从相册中获取

Android九宫格界面实现点击每个格点击跳转界面

点击从相册中获取会

Android九宫格界面实现点击每个格点击跳转界面

下面就是我用到的控件及控件的用法。

 package com.itcast.test03;

 import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast; public class StudentMainActivity extends Activity {
private GridView gridview; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_main);
gridview = (GridView)findViewById(R.id.gridView1);
ArrayList<HashMap<String,Object>> lstImageItem = new ArrayList<HashMap<String,Object>>();
for(int i = 1;i<20;i++){
HashMap<String,Object> map = new HashMap<String, Object>();
if(i==1){
map.put("ItemImage", R.drawable.ic_launcher02);
map.put("ItemText", "实践教学管理系统"); }else if(i==2){
map.put("ItemImage", R.drawable.ic_launcher06);
map.put("ItemText", "毕业设计管理系统"); }else if(i==3){
map.put("ItemImage", R.drawable.ic_launcher02);
map.put("ItemText", "开放实验管理系统"); }else if(i==4){
map.put("ItemImage", R.drawable.ic_launcher03);
map.put("ItemText", "实习实训管理系统"); }else if(i==5){
map.put("ItemImage", R.drawable.ic_launcher05);
map.put("ItemText", "班级事务管理系统"); }else if(i==6){
map.put("ItemImage", R.drawable.ic_launcher06);
map.put("ItemText", "综合实践管理系统"); }else if(i==7){
map.put("ItemImage", R.drawable.ic_launcher04);
map.put("ItemText", "其他事物2管理系统"); }else if(i==8){
map.put("ItemImage", R.drawable.ic_launcher08);
map.put("ItemText", "其他事物3管理系统"); }else if(i==9){
map.put("ItemImage", R.drawable.ic_launcher01);
map.put("ItemText", "其他事物4管理系统"); }else if(i==10){
map.put("ItemImage", R.drawable.ic_launcher02);
map.put("ItemText", "开放实验管理系统"); }else if(i==11){
map.put("ItemImage", R.drawable.ic_launcher03);
map.put("ItemText", "实习实训管理系统"); }else if(i==12){
map.put("ItemImage", R.drawable.ic_launcher05);
map.put("ItemText", "班级事务管理系统"); }else if(i==13){
map.put("ItemImage", R.drawable.ic_launcher06);
map.put("ItemText", "综合实践管理系统"); }else if(i==14){
map.put("ItemImage", R.drawable.ic_launcher04);
map.put("ItemText", "其他事物2管理系统"); }else if(i==15){
map.put("ItemImage", R.drawable.ic_launcher08);
map.put("ItemText", "其他事物3管理系统"); }else if(i==16){
map.put("ItemImage", R.drawable.ic_launcher01);
map.put("ItemText", "其他事物4管理系统"); }
lstImageItem.add(map);
}
SimpleAdapter saImageItems = new SimpleAdapter(this,
lstImageItem,R.layout.next_activity_student_main,
new String[]{"ItemImage","ItemText"},
new int[] {R.id.ItemImage,R.id.ItemText});
gridview.setAdapter(saImageItems);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int index = arg2 + 1;// id是从0开始的,所以需要+1
if (index == 1) { Intent intent = new Intent();
intent.setClass(StudentMainActivity.this,
StudentNextMainActivity.class);
startActivity(intent);
}
if (index == 2) {
Intent intent = new Intent();
intent.setClass(StudentMainActivity.this,
StudentNextMainActivity.class);
startActivity(intent); } }
}); }
}
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <RelativeLayout
android:id="@+id/rl_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="35dp"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:text="欢迎登录软件学院管理系统"/>
</RelativeLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" >
</GridView>
</LinearLayout>
</LinearLayout>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <ImageView
android:id="@+id/ItemImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"/> <TextView
android:id="@+id/ItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/ItemImage"
android:layout_marginTop="24dp"
android:textColor="#000000"
android:textSize="12sp" /> </RelativeLayout>

以上是实现这个界面的代码Android九宫格界面实现点击每个格点击跳转界面

 package com.itcast.test03;

 import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Environment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class StudentNextMainActivity extends Activity
{
private Spinner projectsSpinner = null;
private Spinner kindsSpinner = null;
private Spinner examinesSpinner = null;
ArrayAdapter<String> projectsAdapter = null;
ArrayAdapter<String> kindsAdapter = null;
ArrayAdapter<String> examinesAdapter = null;
static int provincePosition = 3;
private String[] projects = new String[] {"科研训练","素质拓展","社会实践"};
private String[][] kinds = new String[][]
{
{ "项目类", "成果类", "学术活动"},
{ "专业素质", "综合素质"},
{ "无"}
}; private String[][][] examines = new String[][][]
{
{ //科研训练
{"主持或参加科研项目","校大学生创新基金重点项目","校大学生创新基金一般项目"},
{"获奖","著作","专利","论文"},
{"学术交流","学术讲座","课外读书"}
},
{ //素质拓展
{"学科竞赛(含挑战杯)","证书"},
{"开放实验","参加文体活动","组织活动"}
},
{ //社会实践
{"社会实践活动","社团活动","公益劳动","自主创业"}
}
}; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_next_main);
setSpinner();
}
public void pitcure(View view){
screenshot();
}
private void screenshot()
{
// 获取屏幕
View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bmp = dView.getDrawingCache();
if (bmp != null)
{
try {
// 获取内置SD卡路径
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// 图片文件路径
String filePath = sdCardPath + File.separator + "screenshot.png"; File file = new File(filePath);
FileOutputStream os = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
}
}
}
public void next(View view){
Intent intent = new Intent(this,WriteMainActivity.class);
startActivity(intent);
} /*
* 设置下拉框
*/
private void setSpinner()
{
projectsSpinner = (Spinner)findViewById(R.id.spin_projects);
kindsSpinner = (Spinner)findViewById(R.id.spin_kinds);
examinesSpinner = (Spinner)findViewById(R.id.spin_examines); //绑定适配器和值
projectsAdapter = new ArrayAdapter<String>(StudentNextMainActivity.this,
android.R.layout.simple_spinner_item, projects);
projectsSpinner.setAdapter( projectsAdapter);
projectsSpinner.setSelection(2,true); //设置默认选中项,此处为默认选中第3个值 kindsAdapter = new ArrayAdapter<String>(StudentNextMainActivity.this,
android.R.layout.simple_spinner_item, kinds[2]);
kindsSpinner.setAdapter(kindsAdapter);
kindsSpinner.setSelection(0,true); //默认选中第0个
examinesAdapter = new ArrayAdapter<String>(StudentNextMainActivity.this,
android.R.layout.simple_spinner_item, examines[2][0]);
examinesSpinner.setAdapter(examinesAdapter);
examinesSpinner.setSelection(0, true);
projectsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
// 表示选项被改变的时候触发此方法,主要实现办法:动态改变地级适配器的绑定值
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{ kindsAdapter = new ArrayAdapter<String>(
StudentNextMainActivity.this, android.R.layout.simple_spinner_item,kinds[position]);
kindsSpinner.setAdapter(kindsAdapter);
provincePosition = position; //记录当前省级序号,留给下面修改县级适配器时用
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{ } }); //种类下拉监听
kindsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3)
{
examinesAdapter = new ArrayAdapter<String>(StudentNextMainActivity.this,
android.R.layout.simple_spinner_item, examines[provincePosition][position]);
examinesSpinner.setAdapter(examinesAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{ }
});
}
}
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#000000">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:text="学生综合实践模块积分申请表"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="vertical">
<Spinner
android:id="@+id/spin_projects"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spin_kinds"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spin_examines"
android:layout_width="300dp"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="pitcure"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="截图" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="next"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="下一步" /> </RelativeLayout> </LinearLayout>

以上代码是实现这个界面的Android九宫格界面实现点击每个格点击跳转界面

 package com.itcast.test03;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; public class WriteMainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.write_main, menu);
return true;
} }
 <?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"
android:padding="8dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000000">
<Button
android:id="@+id/upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:background="#000000"
android:textSize="18sp"
android:text="发送" />
</RelativeLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="8dp"
android:gravity="left|top"
android:hint="填写你要申请的分数及其他信息......"
android:background="@null" >
</EditText>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/icon_addpic_unfocused" /> <GridView
android:id="@+id/noScrollgridview"
android:layout_width="290dp"
android:layout_height="350dp"
android:layout_marginLeft="5dp"
android:horizontalSpacing="3dp"
android:numColumns="4"
android:scrollbars="none"
android:verticalSpacing="5dp" >
</GridView> </LinearLayout>
</LinearLayout>

以上代码是实现这个界面的Android九宫格界面实现点击每个格点击跳转界面