Andriod 实现一个微信聊天框(一)

这周做了第一次Andriod作业,虽然是对着老师的视频桥的代码,还是遇到了相当多的技术问题和bug,再次记录一下解决问题的过程,以便于后期复习使用。

这次实验的目的是搭建一个类似微信聊天界面的安卓框架,并且实现简单的按钮点击切换页面的效果。
 
不多废话,先上代码
 
MainActivity:

package com.example.jay10_8;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.ImageButton;
// import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private ImageButton mImgWeixin;private ImageButton mImgFrd;private ImageButton mImgAddress;private ImageButton mImgSettings;private Fragment mTab01 = new WeixinFragment();private Fragment mTab02 = new FrdFragment();private Fragment mTab03 = new AddressFragment();private Fragment mTab04 = new SettingFragment();private FragmentManager fm;@Overrideprotected void onCreate(Bundle savedInstanceState) {// 主函数super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();initFragment();initEvent();selectfragment(0);}private void initFragment() {/* 在FrameLayout中添加按钮  */fm = getSupportFragmentManager();FragmentTransaction transaction = fm.beginTransaction();transaction.add(R.id.id_content,mTab01);transaction.add(R.id.id_content,mTab02);transaction.add(R.id.id_content,mTab03);transaction.add(R.id.id_content,mTab04);transaction.commit();}private void initView(){//LinearLayout mTabWeixin = (LinearLayout) findViewById(R.id.weixin_tab1);//LinearLayout mTabFrd = (LinearLayout) findViewById(R.id.friend_tab2);//LinearLayout mTabAddress = (LinearLayout) findViewById(R.id.address_tab3);//LinearLayout mTabSettings = (LinearLayout) findViewById(R.id.setting_tab4);/* 将button设置为对应的ImageButton类型的对象   */mImgWeixin = (ImageButton) findViewById(R.id.weixin_image_button);mImgFrd = (ImageButton) findViewById(R.id.friend_image_button);mImgAddress = (ImageButton) findViewById(R.id.address_image_button);mImgSettings = (ImageButton) findViewById(R.id.setting_image_button);}private void initEvent(){/*设置点击监听程序 (设置图片监听为例)*/mImgWeixin.setOnClickListener(this);mImgFrd.setOnClickListener(this);mImgAddress.setOnClickListener(this);mImgSettings.setOnClickListener(this);}private void hideFragment(FragmentTransaction transaction){transaction.hide(mTab01);transaction.hide(mTab02);transaction.hide(mTab03);transaction.hide(mTab04);}private void selectfragment(int i){/** 执行此函数时,将灰色图标设置为其对应的彩色图标。* */FragmentTransaction transaction = fm.beginTransaction();hideFragment(transaction);switch (i){case 0:transaction.show(mTab01);mImgWeixin.setImageResource(R.drawable.weixin_pressed);break;case 1:transaction.show(mTab02);mImgFrd.setImageResource(R.drawable.friend_pressed);break;case 2:transaction.show(mTab03);mImgAddress.setImageResource(R.drawable.address_pressed);break;case 3:transaction.show(mTab04);mImgSettings.setImageResource(R.drawable.setting_pressed);break;default:break;}transaction.commit();}@Overridepublic void onClick(View v){/** 设置点击按钮动作监听,如果点击到对应按钮,执行对应的selectfragment函数,并将其他图标reset为灰色。* */resetimg();switch (v.getId()){case R.id.weixin_image_button:selectfragment(0);break;case R.id.friend_image_button:selectfragment(1);break;case R.id.address_image_button:selectfragment(2);break;case R.id.setting_image_button:selectfragment(3);break;default:break;}}private void resetimg() {/** 切换图片至暗色.* */mImgWeixin.setImageResource(R.drawable.weixin_normal);mImgFrd.setImageResource(R.drawable.friend_normal);mImgAddress.setImageResource(R.drawable.address_normal);mImgSettings.setImageResource(R.drawable.setting_normal);}}

bottom.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"android:layout_width="match_parent"android:layout_height="60dp"android:background="#B1B1B1"android:baselineAligned="false"android:gravity="bottom"android:orientation="horizontal"android:visibility="visible"><LinearLayoutandroid:id="@+id/weixin_tab1"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="bottom"android:orientation="vertical"android:textColor="#4A4848"><ImageButtonandroid:id="@+id/weixin_image_button"android:layout_width="match_parent"android:layout_height="40dp"android:background="#B1B1B1"android:clickable="false"android:contentDescription="@string/button1"android:onClick="onClick"app:srcCompat="@drawable/weixin_normal" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:clickable="false"android:text="@string/message"android:textColor="#000000"android:textSize="15sp" /></LinearLayout><LinearLayoutandroid:id="@+id/friend_tab2"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="bottom"android:orientation="vertical"><ImageButtonandroid:id="@+id/friend_image_button"android:layout_width="match_parent"android:layout_height="40dp"android:background="#B1B1B1"android:clickable="false"android:contentDescription="@string/button1"android:onClick="onClick"app:srcCompat="@drawable/friend_normal" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:clickable="false"android:text="@string/friend"android:textColor="#000000"android:textSize="15sp" /></LinearLayout><LinearLayoutandroid:id="@+id/address_tab3"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="bottom"android:orientation="vertical"><ImageButtonandroid:id="@+id/address_image_button"android:layout_width="match_parent"android:layout_height="40dp"android:background="#B1B1B1"android:clickable="false"android:contentDescription="@string/button1"android:onClick="onClick"app:srcCompat="@drawable/address_normal" /><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:clickable="false"android:text="@string/people"android:textColor="#000000"android:textSize="15sp" /></LinearLayout><LinearLayoutandroid:id="@+id/setting_tab4"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="bottom"android:orientation="vertical"><ImageButtonandroid:id="@+id/setting_image_button"android:layout_width="match_parent"android:layout_height="40dp"android:background="#B1B1B1"android:clickable="false"android:contentDescription="@string/button1"android:onClick="onClick"app:srcCompat="@drawable/setting_normal" /><TextViewandroid:id="@+id/textView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:clickable="false"android:text="@string/settings"android:textColor="#000000"android:textSize="15sp" /></LinearLayout>
</LinearLayout>

完成效果如下:

上图中的安卓手机界面可以实现按钮点击切换四个tab页面,并在点击不同按钮后恢复原始灰色图标。

在编写过程中,设置Tab监听事件没能成功,但换成Img监听点击就好了,,

视频中的代码最后一个resetImg函数有问题,不能对同一个图片做四次同样的变换,应该设置为对应灰色图标和对应彩色图标。

问题多多,继续努力!

Andriod 实现一个微信聊天框(一)相关推荐

  1. 元素浮动布局,微信聊天框

    元素浮动布局,微信聊天框 知识导入: 文字的浮动布局 浮动元素会脱离网页文档,与其他元素发生重叠,但是与文字内容不会发生重叠即文字环绕效果 由此要注意:浮动元素是不占据空间的, 容器浮动:多个容器达到 ...

  2. 微信聊天框(html+css+js)实现

    微信聊天框(html+css+js)实现 1.来源 这是我在哔哩哔哩的上面的一个js网课的案例,说是有未添加js的版本(也就是只有样式的html),但我找不到,就只能自己写,比较丑. 主要是小图标,都 ...

  3. 动手写一个微信聊天页,有文字,有语音,有图片

    今天给大家分享用网页实现移动端微信聊天记录的功能,效果图如下: 页面布局中,聊天框使用flex布局,聊天气泡通过伪元素实现(前面文章有记录如何实现一个微信聊天气泡框) 语音播放时写的一个css动画,通 ...

  4. 自己动手用Android和Xposed编写一个微信聊天机器人——《微信聊天精灵》实现关键词自动回复。

    出于爱好和需要,想着自己来编写一个微信聊天机器人,能实现以下功能: 能实时获取到微信聊天消息: 能进行文本自动回复: 能够设置关键词: 能够根据关键词匹配,进行内容回复: 能实现聊天消息云端备份: 已 ...

  5. 微信聊天框如何隐藏(微信教程分享)

    微信是很多人经常使用的社交平台.其中还包括很多隐私内容.如果你担心别人借手机的时候翻来覆去.可以隐藏指定的聊天对话框.所以其他人在聊天界面找不到.你知道怎么设置吗?快来一起看看吧. 微信聊天框如何隐藏 ...

  6. 用wxpy做一个微信聊天机器人(详解)

    用python写一个微信聊天机器人可以利用python中的wxpy库或者itchat模块,我在网上看到好多都是使用的itchat,但是我这里使用的是wxpy库,wxpy 在 itchat 的基础上,通 ...

  7. 一个可在微信聊天框中生成短链接的微信公众号

    我们都知道腾讯.新浪的短网址功能并没有官方网站,很多时候我们想生成短链接需要去一些第三方网站,费时费力,有些还带广告或流量劫持! 所以做了这个功能,在聊天框中直接生成短链接,只需要发送网址,一秒内就会 ...

  8. 企业微信聊天框中点击自建应用获取当前聊天外部联系人userid,wx.agentConfig调用

    目录 企业微信自建应用获取当前聊天外部联系人userid,wx.agentConfig调用 调用agentConfig wx.agentConfig 和 wx.config区别 wx.agentCon ...

  9. php类似微信聊天框,仿微信聊天功能

    摘要: 微信聊天 window.onload = () => { // 获取按钮 let btn = document.getElementById('btn'); // 获取输入框 let t ...

最新文章

  1. WCF 和 ASP.NET Web API
  2. 基于外卖评论的舆情风控
  3. csv文件怎么转成excel_怎么把word转成excel
  4. 吉林高考成绩查询2021年几号公布,2021年吉林高考成绩查询时间及查分方式
  5. 一个包含嵌套递归数据结构的对象的排序实现
  6. Jenkins打包之本地远程自动打包教程
  7. Web笔记-通过版本号控制客户端浏览器中的缓存
  8. 3、Django下载与简介
  9. Top Down Operator Precedence - 自顶向下算符优先分析法
  10. CS231n李飞飞计算机视觉 卷积神经网络详解上
  11. NOPI导出到excel
  12. 打印机加粉出现的问题:打印机m7206清零
  13. Hugo博客搭建配置
  14. ubuntu 设置静态路由_Ubuntu添加静态路由
  15. 霍尔 磁电 光电式测数传感器的优缺点比较
  16. Verilog语言要素(二)
  17. 手把手教你使用Typecho搭建自己的个人博客
  18. [旭日x3] 动手实践之bpu_rezie以及简化cpp编译流程
  19. Kubernetes进阶使用(二)
  20. 学校机房网线插入自己电脑的设置方法

热门文章

  1. 响应式布局——Bootstrap
  2. springboot接入华为云短信
  3. centos8重启网卡服务
  4. 如何实现简单粗暴靠谱的直播抓娃娃方案
  5. mysql 电商实战_SQL电商数据分析实战
  6. 50台同样配置的计算机装系统,几十台PC如何同时安装系统
  7. 区块链的硬分叉、软分叉介绍
  8. 数据结构大作业——银行排队系统
  9. Excel 有哪些可能需要熟练掌握而很多人不会的技能2
  10. 网页被劫持跳转怎么办?发布网修复方法