Android软件开发(倾心家教)全过程
有登录,注册界面,底部导航栏,各个界面跳转笔记十分的详细,代码基本上传,因此篇幅较长。希望对大家android软件开发有点帮助。
视频如下:
android自制软件完成视频
文章目录
脑图:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/81751a70f060652f64a67b54ecbc3104.jpeg#pic_center)
效果图
![](https://i-blog.csdnimg.cn/blog_migrate/dbcada117b2f219eae7c6adc1aa95e46.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/06df4d369b55e9f29538efe42b0ded5f.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/071cfdac5536008599b335c3ea84eaff.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/5b13ae69f6fd5ba30f7a8d8132352d40.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/38ccc270564036a66ad405ac9b136bb7.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/6d70220b89a521930eee1b5a39528157.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/1ca63bd4d0f2c14dd0a9a6a5e9906920.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/d070323bcdc4a553924eadd3c63baa7e.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/1f4d7eaaa9dcf08dc57d90eef0aa66c1.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/b88486af0c946ea88c57a4a67452d456.png)
一.登录界面xml
activity_main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/a"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:paddingTop="20dp"
android:background="#99404348"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="倾心家教"
android:textColor="#FFFFFF"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:background="#99000000">
<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#9F9FA0"
android:textColorHint="#9F9FA0"
android:hint="your username"/>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#83738F"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"></View>
<EditText
android:id="@+id/editPwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:hint="your password"
android:textColor="#9F9FA0"
android:textColorHint="#9F9FA0"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:paddingTop="20dp"
android:background="#99404348"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="忘记密码?"
android:textColor="#DDDDDD"
android:textSize="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="点击这里"
android:textColor="#FFFFFF"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/buttonstyle"
android:text="登录"/>
<Button
android:id="@+id/btnReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:layout_marginLeft="10dp"
android:background="@drawable/buttonstyle"
android:layout_weight="1"
android:text="注册"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
二.登录界面java
MainActivity.java 可以实现接收注册界面的用户名和密码,同时能判断用户名,和密码是否正确,并通过弹框来提醒。
package com.example.teacher11;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button login, reg;
private EditText editText1, editText2;
private String username, userpwd;
private final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.btnLogin);
reg = (Button) findViewById(R.id.btnReg);
editText1 = (EditText) findViewById(R.id.editName);
editText2 = (EditText) findViewById(R.id.editPwd);
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,registerActivity.class);
startActivityForResult(intent,REQUEST_CODE);
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, successActivity.class);
String name = editText1.getText().toString();
String pwd = editText2.getText().toString();
if (name.equals(username) && pwd.equals(userpwd)) {
intent.putExtra("name", name);
intent.putExtra("pwd", pwd);
startActivity(intent);
}
if (!name.equals(username)) {
Toast.makeText(MainActivity.this, "抱歉,用户名不对", Toast.LENGTH_LONG).show();
}
if (!pwd.equals(userpwd)) {
Toast.makeText(MainActivity.this, "抱歉,密码不对", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==REQUEST_CODE){
String name=data.getStringExtra("name");
String pwd=data.getStringExtra("pwd");
editText1.setText("您的用户名是:"+name);
editText2.setText("您的密 码是 :"+pwd);
username=editText1.getText().toString();
userpwd=editText2.getText().toString();
}
}
}
三.注册界面
接下来的是注册界面,和登录界面同理,布局文件就不展示了,直接看java,将用户的用户名和密码传回到登录。
package com.example.teacher11;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class registerActivity extends AppCompatActivity {
private Button zcan;
private EditText zceditext1, zcedittext2, zcedittext3;
private static final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
zcan = (Button) findViewById(R.id.zcbtnReg);
zceditext1 = (EditText) findViewById(R.id.zcedtName);
zcedittext2 = (EditText) findViewById(R.id.zcedtPwd);
zcedittext3 = (EditText) findViewById(R.id.zcedtRepwd);
zcan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
String name = zceditext1.getText().toString();
String pwd = zcedittext2.getText().toString();
String repwd = zcedittext3.getText().toString();
if (!"".equals(pwd) && pwd.equals(repwd)) {
intent.putExtra("name", name);
intent.putExtra("pwd", pwd);
setResult(REQUEST_CODE, intent);
registerActivity.this.finish();
} else {
Toast.makeText(registerActivity.this, "密码输入不一致", Toast.LENGTH_LONG).show();
}
}
});
}
}
四.底部导航栏
我是通过fragment和textview来实现底部导航栏的跳转
4.1 activity_success.xml代码如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/view_line"/>
<View
android:id="@+id/view_line"
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#DCDBDB"
android:layout_above="@+id/rl_bottom"/>
<LinearLayout
android:id="@+id/rl_bottom"
android:layout_width="392dp"
android:layout_height="72dp"
android:layout_alignParentBottom="true"
android:background="#F2F2F2"
android:orientation="horizontal"
android:paddingTop="1dp"
android:paddingBottom="1dp">
<TextView
android:id="@+id/tv_main"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/tab_item_main_img_selector"
android:drawablePadding="1dp"
android:focusable="true"
android:gravity="center"
android:text="首页"
android:textColor="@drawable/tab_item_txt_selector" />
<TextView
android:id="@+id/tv_mood"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/tab_item_message_img_selector"
android:drawablePadding="5dp"
android:focusable="true"
android:gravity="center"
android:text="老师"
android:textColor="@drawable/tab_item_txt_selector" />
<TextView
android:id="@+id/tv_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/tab_item_mood_img_selector"
android:drawablePadding="5dp"
android:focusable="true"
android:gravity="center"
android:text="消息"
android:textColor="@drawable/tab_item_txt_selector" />
<TextView
android:id="@+id/tv_person"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:drawableTop="@drawable/tab_item_person_img_selector"
android:drawablePadding="5dp"
android:focusable="true"
android:gravity="center"
android:text="我的"
android:textColor="@drawable/tab_item_txt_selector" />
</LinearLayout>
</RelativeLayout>
4.2 successActivity.java代码如下:
package com.example.teacher11;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import static com.example.teacher11.R.layout.activity_main_fragment;
/**
* 通过切换选项控制页面更新
* @author
* @create
*/
public class successActivity extends FragmentActivity {
private MainFragment mainFragment;
private MoodFragment moodFragment;
private MessageFragment messageFragment;
private PersonFragment personFragment;
private int currentId = R.id.tv_main;
private TextView tvMain, tvMood, tvMessage, tvPerson;
//index
private Button benan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_success);
// 首页标签默认选中
tvMain = (TextView) findViewById(R.id.tv_main);
tvMain.setSelected(true);
tvMood = (TextView) findViewById(R.id.tv_mood);
tvMessage = (TextView) findViewById(R.id.tv_message);
tvPerson = (TextView) findViewById(R.id.tv_person);
//App启动的时候加载首页页面
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.main_container, mainFragment).commit();
tvMain.setOnClickListener(tabClickListener);
tvMood.setOnClickListener(tabClickListener);
tvMessage.setOnClickListener(tabClickListener);
tvPerson.setOnClickListener(tabClickListener);
}
//private OnClickListener onClickListener =new OnClickListener() {
// @Override
// public void onClick(View v) {
// switch (v.getId()) {
// case R.id.an:
// Intent intent = new Intent(successActivity.this, textActivity.class);
// startActivity(intent);
// break;
// }
// }
//};
/**
* 通过单击事件调用对应方法改变TextView的状态和fragment
* 更新当前项的id(主要根据id标记当前项的位置)
*/
private OnClickListener tabClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() != currentId) {
changeSelect(v.getId());
changeFragment(v.getId());
currentId = v.getId();
}
}
};
/**
* 切换fragment方法
* @param resId
*/
private void changeFragment(int resId) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
//调用方法隐藏fragment
hideFragments(transaction);
//声明fragment并且添加到页面
if (resId == R.id.tv_main) {
if (mainFragment == null) {
mainFragment = new MainFragment();
transaction.add(R.id.main_container, mainFragment);
} else {
transaction.show(mainFragment);
}
} else if (resId == R.id.tv_mood) {
if (moodFragment == null) {
moodFragment = new MoodFragment();
transaction.add(R.id.main_container, moodFragment);
} else {
transaction.show(moodFragment);
}
} else if (resId == R.id.tv_message) {
if (messageFragment == null) {
messageFragment = new MessageFragment();
transaction.add(R.id.main_container, messageFragment);
} else {
transaction.show(messageFragment);
}
} else if (resId == R.id.tv_person) {
if (personFragment == null) {
personFragment = new PersonFragment();
transaction.add(R.id.main_container, personFragment);
} else {
transaction.show(personFragment);
}
}
transaction.commit();
}
/**
* 点击事件隐藏所有fragment
* @param transaction
*/
private void hideFragments(FragmentTransaction transaction) {
if (mainFragment != null)
transaction.hide(mainFragment);
if (moodFragment != null)
transaction.hide(moodFragment);
if (messageFragment != null)
transaction.hide(messageFragment);
if (personFragment != null)
transaction.hide(personFragment);
}
/**
* 单击事件改变textview的状态
* @param resId
*/
private void changeSelect(int resId) {
//选项全部设置为未选中
tvMain.setSelected(false);
tvMood.setSelected(false);
tvMessage.setSelected(false);
tvPerson.setSelected(false);
//切换选中项的状态
switch (resId) {
case R.id.tv_main:
tvMain.setSelected(true);
break;
case R.id.tv_mood:
tvMood.setSelected(true);
break;
case R.id.tv_message:
tvMessage.setSelected(true);
break;
case R.id.tv_person:
tvPerson.setSelected(true);
break;
}
}
}
五.各个fragment设计
点击底部导航栏实现跳转到各个fragment中,接下来来写4个Fragment
5.1.第一个fragment
是MainFragment,activity_main_fragment.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">
<!-- 主要内容-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="#87CEFA"
android:orientation="horizontal">
<!-- 显示地点-->
<TextView
android:id="@+id/dizhi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#FFFFFF"
android:textSize="18dp"
android:text="马鞍山"
/>
<!-- 显示搜索框-->
<!-- <SearchView-->
<!-- android:layout_width="1dp"-->
<!-- android:layout_height="40dp"-->
<!-- android:layout_weight="4"-->
<!-- android:layout_marginTop="6dp"-->
<!-- android:layout_marginRight="10dp"-->
<!-- android:background="@color/whiteColor"-->
<!-- android:singleLine="true">-->
<!-- </SearchView>-->
<EditText
android:id="@+id/url"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_weight="6"
android:layout_marginTop="6dp"
android:layout_marginRight="10dp"
android:background="@color/whiteColor"/>
<ImageView
android:id="@+id/sousuo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/sousuo"/>
</LinearLayout>
<!-- 课程-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#FFFFFF"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<!-- 显示课程图标和名称-->
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/math"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/math" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="数学"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/yingyu"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/yingyu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="英语"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/yuwen"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/yuwen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="语文"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/wuli"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/wuli" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="物理"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
<!-- 开始第二行-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<!-- 显示课程图标和名称-->
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/mzhengzhi"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/zhengzhi" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="化学"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/huaxue"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/huaxue" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="化学"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/shengwu"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/shegnwu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="生物"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/dili"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/dili" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="地理"
android:textColor="#87CEFA"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- 以上是课程图标-->
<!--以下是滚动消息-->
<include
layout="@layout/index_toutiao_scroll" />
<include
layout="@layout/index_teacher"/>
<!--热门名师图片跳转-->
<include layout="@layout/famous_teacher"/>
</LinearLayout>
</LinearLayout>
这个界面上,搜索栏可以实现网址搜索。滚动消息是以走马灯的形式,还有图片的轮播效果。
5.1.1.网址搜索要实现网络资源的请求
要在AndroidManifest.xml的Manifest下面,插入这一行代码
<uses-permission android:name="android.permission.INTERNET"/>
5.1.2.滚动消息(走马灯)
index_touxiao_scroll.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="50dp"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 名师一对一辅导啦! 欢迎大家加入到倾心家教! 好好学习,天天向上!"
android:textColor="#000000"
android:textSize="20dp"
android:id="@+id/gundon"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:focusable="true"
android:scrollHorizontally="true"
android:focusableInTouchMode="true"
android:singleLine="true"/>
</LinearLayout>
ps:有能力可以实现将消息自动滚动起来,本人小白,表示无力,至于热门名师,index_teacher.xml里面就是一个简单的TextView,代码就省略了)
5.1.3.图片滚动效果
famous_teacher.xml代码入下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="300dip" >
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:layout_gravity="bottom"
android:background="#33000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图片标题"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:orientation="horizontal" >
<View
android:id="@+id/dot_0"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_yes"/>
<View
android:id="@+id/dot_1"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_no"/>
<View
android:id="@+id/dot_2"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_no"/>
<View
android:id="@+id/dot_3"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_no"/>
<View
android:id="@+id/dot_4"
android:layout_width="5dip"
android:layout_height="5dip"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:background="@drawable/dot_no"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
第一个fragment的java代码
MainFragment总的代码入下:
package com.example.teacher11;
import com.example.teacher11.R;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
//import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainFragment extends Fragment {
private ImageView btnsousuo;
private EditText edtUrl;
//ceshi初始化
private View mView;
private ViewPager mViewPaper;
private List<ImageView> images;
private List<View> dots;
private int currentItem;
//记录上一次点的位置
private int oldPosition = 0;
//存放图片的id
private int[] imageIds = new int[]{
R.drawable.lunbo1,
R.drawable.lunbo2,
R.drawable.lunbo3,
R.drawable.lunbo4,
R.drawable.lunbo5
};
//存放图片的标题
private String[] titles = new String[]{
"窗明几净",
"孜孜不倦",
"大赞园丁",
"梦幻校园",
"遵守纪律"
};
private TextView title;
private ViewPagerAdapter adapter;
private ScheduledExecutorService scheduledExecutorService;
//jiezhi
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
mView=inflater.inflate(R.layout.activity_main_fragment, null);
setView();
return mView;
}
//ceshi
private void setView(){
mViewPaper = (ViewPager)mView.findViewById(R.id.vp);
//显示的图片
images = new ArrayList<ImageView>();
for(int i = 0; i < imageIds.length; i++){
ImageView imageView = new ImageView(getActivity());
imageView.setBackgroundResource(imageIds[i]);
images.add(imageView);
}
//显示的小点
dots = new ArrayList<View>();
dots.add(mView.findViewById(R.id.dot_0));
dots.add(mView.findViewById(R.id.dot_1));
dots.add(mView.findViewById(R.id.dot_2));
dots.add(mView.findViewById(R.id.dot_3));
dots.add(mView.findViewById(R.id.dot_4));
title = (TextView) mView.findViewById(R.id.title);
title.setText(titles[0]);
adapter = new ViewPagerAdapter();
mViewPaper.setAdapter(adapter);
mViewPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
title.setText(titles[position]);
dots.get(position).setBackgroundResource(R.drawable.dot_yes);
dots.get(oldPosition).setBackgroundResource(R.drawable.dot_no);
oldPosition = position;
currentItem = position;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
/*定义的适配器*/
public class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return images.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
// view.removeView(view.getChildAt(position));
// view.removeViewAt(position);
view.removeView(images.get(position));
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
// TODO Auto-generated method stub
view.addView(images.get(position));
return images.get(position);
}
}
/**
* 利用线程池定时执行动画轮播
*/
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleWithFixedDelay(
new ViewPageTask(),
2,
2,
TimeUnit.SECONDS);
}
/**
* 图片轮播任务
* @author liuyazhuang
*
*/
private class ViewPageTask implements Runnable{
@Override
public void run() {
currentItem = (currentItem + 1) % imageIds.length;
mHandler.sendEmptyMessage(0);
}
}
/**
* 接收子线程传递过来的数据
*/
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
mViewPaper.setCurrentItem(currentItem);
};
};
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(scheduledExecutorService != null){
scheduledExecutorService.shutdown();
scheduledExecutorService = null;
}
}
//戒指
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnsousuo=(ImageView) mView.findViewById(R.id.sousuo);
edtUrl=(EditText)mView.findViewById(R.id.url);
btnsousuo.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
String strUrl = edtUrl.getText().toString();
Uri uri = Uri.parse(strUrl);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
});
}
//
}
(ps: dot_yes是图片选中播放时候的亮点,dot_no反之,百度一下图片就行)
5.2第二个是MoodFragment
5.2.1activity_mood_fragment
布局文件代码入下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E8E8E8"
android:orientation="vertical">
<!-- 显示要查找老师的信息标题-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:background="#87CEFA"
android:orientation="horizontal">
<!-- 返回箭头-->
<ImageView
android:id="@+id/arrback"
android:layout_width="24dp"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_weight="0.5"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowleft" />
<!-- 科目文字-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:layout_marginTop="12dp"
android:text="查找教师"/>
</LinearLayout>
</LinearLayout>
<!-- 间隔线-->
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#CFCFCF"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/subject_name"
android:layout_gravity="center_horizontal"
android:text="科目"
android:textColor="#8B8B7A"
android:background="@android:color/transparent"
android:drawableRight="@drawable/arrowdown"/>
</LinearLayout>
<!--间隔线-->
<ImageView
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_marginTop="5dp"
android:background="#CFCFCF"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="vertical">
<Button
android:id="@+id/grade"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:text="年 级"
android:textColor="#8B8B7A"
android:background="@android:color/transparent"
android:drawableRight="@drawable/arrowdown"/>
</LinearLayout>
</LinearLayout>
<!--间隔线-->
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#CFCFCF"/>
<!-- 预约教师信息-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_yuwen"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 语文老师"
android:textSize="28dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_shuxu"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 数学老师"
android:textSize="28dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou3"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_dili"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 地理老师"
android:textSize="28dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou4"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_shengwu"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 生物老师"
android:textSize="28dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou5"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_yingyu"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 英语老师"
android:textSize="28dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou6"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_huaxue"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 化学老师"
android:textSize="28dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou7"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_zhengzhi"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 政治老师"
android:textSize="28dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/laoshitou8"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/mood_wuli"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 物理老师"
android:textSize="28dp"/>
</LinearLayout>
<!-- //listview-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/friendlist"
android:dividerHeight="3dp">
</ListView>
</LinearLayout>
点击各个老师,弹框可以出现对应学科的名人名言
5.2.2 MoodFragment.java
MoodFragment对应的java代码入下:
package com.example.teacher11;
import com.example.teacher11.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MoodFragment extends Fragment {
private TextView btn_mood_yuwen;
private TextView btn_mood_shuxue;
private TextView btn_mood_yingyu;
private TextView btn_mood_wuli;
private TextView btn_mood_huaxue;
private TextView btn_mood_zhengzhi;
private TextView btn_mood_dili;
private TextView btn_mood_shengwu;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_mood_fragment, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView btn_mood_yuwen=(TextView)getActivity().findViewById(R.id.mood_yuwen);
TextView btn_mood_shuxue=(TextView)getActivity().findViewById(R.id.mood_shuxu);
TextView btn_mood_yingyu=(TextView)getActivity().findViewById(R.id.mood_yingyu);
TextView btn_mood_wuli=(TextView)getActivity().findViewById(R.id.mood_wuli);
TextView btn_mood_zhengzhi=(TextView)getActivity().findViewById(R.id.mood_zhengzhi);
TextView btn_mood_shengwu=(TextView)getActivity().findViewById(R.id.mood_shengwu);
TextView btn_mood_huaxue=(TextView)getActivity().findViewById(R.id.mood_huaxue);
TextView btn_mood_dili=(TextView)getActivity().findViewById(R.id.mood_dili);
btn_mood_yuwen.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"天行健,君子以自强不息!",Toast.LENGTH_LONG).show();
}
});
btn_mood_shuxue.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"数学,是科学的女皇,数论,是数学的女皇!",Toast.LENGTH_LONG).show();
}
});
btn_mood_yingyu.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"For man is man and master of his fate. 人就是人,是自己命运的主人。",Toast.LENGTH_LONG).show();
}
});
btn_mood_wuli.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"物理定律不能单靠“思维”来获得,还应致力于观察和实验。",Toast.LENGTH_LONG).show();
}
});
btn_mood_zhengzhi.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"政治是一种科学,是一种艺术。",Toast.LENGTH_LONG).show();
}
});
btn_mood_shengwu.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"物竞天择,适者生存!",Toast.LENGTH_LONG).show();
}
});
btn_mood_huaxue.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"化学家需要精细,必须杜绝含糊其词的“about”!",Toast.LENGTH_LONG).show();
}
});
btn_mood_dili.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"高岸为谷,深谷为陵。",Toast.LENGTH_LONG).show();
}
});
}
}
(ps: 不足之处:点击学科和年纪应该出现下拉框,没有实现。java中代码过于冗余可以简化一下,个人比较懒,都是复制粘贴的。)
5.3第三个是MessageFragment
5.3.1activity_message_fragment.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F5F5F5">
<!-- 消息标题-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:orientation="horizontal"
android:background="#87CEFA">
<!-- 返回箭头-->
<ImageView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.5"
android:layout_marginTop="10dp"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowleft"
android:background="#87CEFA"/>
<!-- 消息-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="5"
android:orientation="vertical"
android:background="#87CEFA">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:layout_marginTop="12dp"
android:text="消息通知"/>
</LinearLayout>
</LinearLayout>
<!-- 预约教师信息-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/teacher1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/order_teacher"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 预约老师消息"
android:textSize="28dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 我要提建议-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/jianyi1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/jianyi"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 我要提建议"
android:textSize="28dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 消息论坛-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/luntan1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/luntan"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 消息论坛"
android:textSize="28dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 优惠信息-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/youhuiquan2"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/youhui"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 优惠信息"
android:textSize="28dp" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/an"
android:text="开发者"/>
</LinearLayout>
5.3.2MessageFragment.java
总代码如下:
package com.example.teacher11;
import com.example.teacher11.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MessageFragment extends Fragment {
private Button an;
private TextView btn_order_teacher;
private TextView btn_mes_jianyi;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_messag_fragment, container, false);
}
// 想要在fragment中实现监听点击事件,得用onActivity
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView btn_order_teacher=(TextView)getActivity().findViewById(R.id.order_teacher);
btn_order_teacher.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(getActivity(),mes_order_teacher.class);
startActivity(intent);
}
});
TextView btn_mes_jianyi=(TextView)getActivity().findViewById(R.id.jianyi);
btn_mes_jianyi.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(getActivity(),mes_jianyi.class);
startActivity(intent);
}
});
Button btnan=(Button)getActivity().findViewById(R.id.an);
btnan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(getActivity(),textActivity.class);
startActivity(intent);
}
});
}
}
5.3.3 下一级跳转
此界面点击预约老师,我要提建议和开发者按钮跳转到下一级activity中(对应为:mes_order_teacher,mes_jianyi,textActivity)。
activity_mes_jianyi.xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mes_jianyi"
android:orientation="vertical"
android:background="#F5F5F5">
<!-- 建议标题-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:orientation="horizontal"
android:background="#87CEFA">
<!-- 返回箭头-->
<ImageView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.5"
android:layout_marginTop="10dp"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowleft"
android:background="#87CEFA"/>
<!-- 消息-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="5"
android:orientation="vertical"
android:background="#87CEFA">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="22sp"
android:textColor="#FFFFFF"
android:layout_marginTop="12dp"
android:text="我的建议"/>
</LinearLayout>
</LinearLayout>
<!-- 建议科目-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:text="科目:"/>
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="4"
android:id="@+id/spinner1"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:background="#FFFFFF">
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:text="主题:"/>
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="4"
android:id="@+id/edit_zhuti"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:background="#FFFFFF">
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:text="描述:"/>
<EditText
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="4"
android:id="@+id/edit_miaoshu"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="@+id/suggest_report"
android:text="发表意见"
android:textColor="#FFFFFF"
android:layout_gravity="center"/>
</LinearLayout>
(ps: activity_mes_teacher.xml和activity_teacher.xml布局文件大家可以自己发挥想象,不足之处:应该为每一个控件实现跳转,还应该在下一级的向左的箭头实现点击事件,能返回到上一级中来)
5.4第四个是PersonFragment
5.4.1activity_person_fragment.xml
布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="110dp"
android:id="@+id/user_message"
android:background="#87CEFA"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="70dp"
android:id="@+id/icon"
android:layout_weight="1"
android:layout_gravity="center"
android:contentDescription="@string/app_name"
android:src="@drawable/hugh"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/user_id"
android:layout_gravity="center"
android:layout_weight="4"
android:text="用户名:云澈"
android:textColor="#FFFFFF"
android:layout_marginLeft="30dp"
android:textSize="20sp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<include layout="@layout/me_money"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="600dp"
android:layout_marginTop="20dp"
android:background="#F5F5F5"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#CFCFCF"/>
<!-- 我的老师-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/me_teacher"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/me_teacher"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 我的老师"
android:textSize="28dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 我的评价-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/me_pingjia"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/me_pingjia"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 我的评价"
android:textSize="28dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 我的回答-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/me_answer"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/me_answer"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 我的回答"
android:textSize="28dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 我的课程-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/me_kecheng"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/order_teacher"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 我的课程"
android:textSize="28dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 学习计划和总结-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/me_xuexijiahua"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/学习"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 学习计划和总结"
android:textSize="28dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
<!-- 我的课程-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#FFFFFF"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/me_shezhi"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/me_shezhi"
android:layout_weight="5"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text=" 设置"
android:textSize="28dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:contentDescription="@string/app_name"
android:src="@drawable/arrowright"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
其中me_money.xml布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:background="#FFFFFF">
<!--我的订单-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/order"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:contentDescription="@string/app_name"
android:src="@drawable/order1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="我的订单"
android:layout_marginTop="2dp"
android:textSize="16sp"/>
</LinearLayout>
<!--钱包 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/money"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:contentDescription="@string/app_name"
android:src="@drawable/money"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="钱包"
android:layout_marginTop="2dp"
android:textSize="16sp"/>
</LinearLayout>
<!--优惠券-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/优惠券"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:contentDescription="@string/app_name"
android:src="@drawable/youhuiquan1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="我的订单"
android:layout_marginTop="2dp"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
5.4.2PersonFragment.java
代码如下:
package com.example.teacher11;
import com.example.teacher11.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class PersonFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.activity_person_fragment, container, false);
}
}
(ps:不足之处:同上,应该为每一个控件实现点击跳转到下一级)
本软件没有后端数据库,没法实现功能。
六.总结
第一次直接复制肯定是会报错的,大家要改改自己的图片名称等一些错误。我个人也是写了好几个晚上,看了好长时间的书本,开始几天都没有敲代码,直到看懂一些代码,同时也查阅了很多百度,我要十分感谢CSDN社区,很多问题都是这里查询找到解决办法的。越到问题不要放弃,慢慢摸索,坚持下来,一定能写完的。
本人也是第一次写android软件,同时也是第一次写那么长的笔记总结。其中有很多不完善的,不足之处。欢迎大家评论指正。