学习目标:
请根据课程实操实现APP门户界面框架设计,至少包含4个tab页,能实现tab页之间的点击切换;
学习内容:
1.请根据课程实操实现APP门户界面框架设计,至少包含4个tab页,能实现tab页之间的点击切换;
2.使用布局(layouts)和分段(fragment),对控件进行点击监听;
功能说明:
进入MainActivity文件声明
将button与图片联系起来,因为监听是通过button来的,因此要实现的功能就是通过点击图片完成监听
initFragment函数
用来添加四个用来切换的界面
showFragment函数
控制图片颜色的变换,点击一个图片之后该图片就会变成绿色
hideFragment函数
作用是在显示一个界面之前将所有的界面都隐藏
事件监听控制
实现点击bottom上的四个按钮实现切换
onClick函数
对点击某个区域起反应的函数/一个监听函数
里面嵌套了一个函数resetImg
提供灰暗图标的图片,让点击过后的图片恢复原色
实验代码:
```java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Fragment wechatFragment = new WechatFragment();
private Fragment friendFragment = new FriendFragment();
private Fragment contectFragment = new ContectFragment();
private Fragment configFragment = new ConfigFragment();
private FragmentManager fragmentManager;
private LinearLayout linearLayout1;
private LinearLayout linearLayout2;
private LinearLayout linearLayout3;
private LinearLayout linearLayout4;
private ImageView v1;
private ImageView v2;
private ImageView v3;
private ImageView v4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
linearLayout1 = findViewById(R.id.linearLayout1);
linearLayout2 = findViewById(R.id.linearLayout2);
linearLayout3 = findViewById(R.id.linearLayout3);
linearLayout4 = findViewById(R.id.linearLayout4);
linearLayout1.setOnClickListener(this);
linearLayout2.setOnClickListener(this);
linearLayout3.setOnClickListener(this);
linearLayout4.setOnClickListener(this);
v1 = findViewById(R.id.imageView1);
v2 = findViewById(R.id.imageView2);
v3 = findViewById(R.id.imageView3);
v4 = findViewById(R.id.imageView4);
initFragment();
}
protected void initFragment(){
fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.content,wechatFragment);
transaction.add(R.id.content,friendFragment);
transaction.add(R.id.content,contectFragment);
transaction.add(R.id.content,configFragment);
hideFragment(transaction);
transaction.show(wechatFragment);
transaction.commit();
}
protected void hideFragment(FragmentTransaction transaction){
transaction.hide(wechatFragment);
transaction.hide(friendFragment);
transaction.hide(contectFragment);
transaction.hide(configFragment);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.linearLayout1:
showFragment(1);
break;
case R.id.linearLayout2:
showFragment(2);
break;
case R.id.linearLayout3:
showFragment(3);
break;
case R.id.linearLayout4:
showFragment(4);
break;
default:
break;
}
}
protected void showFragment(int i){
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragment(transaction);
switch (i) {
case 1:
setEmpty();
v1.setImageResource(R.drawable.wx1);
transaction.show(wechatFragment);
break;
case 2:
setEmpty();
v2.setImageResource(R.drawable.fri1);
transaction.show(friendFragment);
break;
case 3:
setEmpty();
v3.setImageResource(R.drawable.lxr1);
transaction.show(contectFragment);
break;
case 4:
setEmpty();
v4.setImageResource(R.drawable.my1);
transaction.show(configFragment);
break;
default:
break;
}
transaction.commit();
}
protected void setEmpty(){
v1.setImageResource(R.drawable.wx);
v2.setImageResource(R.drawable.fri);
v3.setImageResource(R.drawable.lxr);
v4.setImageResource(R.drawable.my);
}
}
git仓库
https://gitee.com/tu-haidong/as/tree/master/%E7%AC%AC%E4%B8%80%E6%AC%A1%E4%BD%9C%E4%B8%9A
实验截图:
心得体会:
第一次的实验初次了解到了安卓开发技术,了解了as的相关功能的使用,学习到了相关的实验操作。在接触到前端的开发中,我明白,熟练的运用控件的应用需要多加练习,然后掌握java语法基础才能有的放矢。