寫在前面
由于本人初學(xué)階段,寫這篇博客是總結(jié)所學(xué)的知識點阔拳,為后面的進階打好基礎(chǔ)
有任何關(guān)于代碼和表述問題,歡迎評論區(qū)指出
樓主近期在學(xué)習(xí)關(guān)于安卓中Fragment
和ListView
中的知識宾濒,按照老師的要求模仿一下QQ界面 要求功能
- 有登錄界面
- 密碼不對提示密碼不對
- 賬號密碼任一為空提示用戶不能為空
- 登錄成功提示登錄成功
- 可以實現(xiàn)賬號密碼記住功能
- 有三個界面可以點擊底部按鈕實現(xiàn)頁面的切換
- 實現(xiàn)按鈕選中狀態(tài)和未選中狀態(tài)不一樣
- 聯(lián)系人界面
- 信息界面
- 狀態(tài)界面
- 發(fā)送信息功能
- 點擊信息界面中的任意消息可以進入發(fā)消息界面
- 可以實現(xiàn)點擊發(fā)送按鈕將所輸入的文字顯示在屏幕中
提示:本人用的IDE開發(fā)環(huán)境是Android Studio
API是30
目錄結(jié)構(gòu)一覽
res中的文件
1. 登錄界面
- 登錄界面的布局文件
activity_main.xml
所用圖標(biāo)都可以在阿里巴巴圖標(biāo)網(wǎng)找到iconfont-阿里巴巴矢量圖標(biāo)庫
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
tools:ignore="UselessParent">
<TextView
android:drawableLeft="@drawable/qq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="120dp"
android:gravity="center"
android:text="QQ"
android:textColor="#000000"
android:textSize="35sp"
app:drawableStartCompat="@drawable/qq"
android:drawableStart="@drawable/qq"
tools:ignore="UseCompatTextViewDrawableXml"
android:layout_marginStart="120dp">
</TextView>
<EditText
android:id="@+id/userNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:hint="請輸入用戶名/賬號/手機號"
android:inputType="text"
android:padding="5dp">
</EditText>
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:padding="5dp">
</EditText>
<ImageButton
android:id="@+id/loginButton"
android:layout_marginTop="60dp"
android:layout_gravity="center"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/login"
android:background="#fff">
</ImageButton>
</LinearLayout>
</RelativeLayout>
所對應(yīng)的MainActivity
- 其中的判斷通過if語句判斷用戶是否輸入和輸入正確
- 提示 通過
Toast
的方式提示
package com.czie.qq;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Context context;
private EditText userNameEditText, passwordEditText;
private ImageButton loginButton;
private ShareHelper shareHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
shareHelper=new ShareHelper(context);
initview();
}
private void initview() {
userNameEditText = findViewById(R.id.userNameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//進行登錄頁面的處理
String username = userNameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (username.length() > 0) {
if (username.equals("zk")) {
if (password.length() > 0) {
if (password.equals("123")) {
// 對賬號和密碼進行保存
shareHelper.save("username",username);
shareHelper.save("password",password);
startActivity(new Intent(MainActivity.this, HomeActivity.class));
Toast.makeText(MainActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "密碼不正確", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,"請?zhí)顚懨艽a",Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,"用戶名不正確",Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this,"請?zhí)顚懹脩裘?,Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
userNameEditText.setText(shareHelper.read("username"));
passwordEditText.setText(shareHelper.read("password"));
}
}
2. 記住密碼功能
- 實現(xiàn)第一次登錄成功后欣簇,再次登錄會記住賬號和密碼的功能
這個功能我們用SharedPreferences
簡單了解下什么是SharedPreferences
- SharedPreferences是Android平臺上一個輕量級的存儲輔助類,用來保存應(yīng)用的一些常用配置翘狱,它提供了string秘案,set,int潦匈,long阱高,float,boolean六種數(shù)據(jù)類型茬缩。最終數(shù)據(jù)是以xml形式進行存儲赤惊。在應(yīng)用中通常做一些簡單數(shù)據(jù)的持久化緩存。
package com.czie.qq;
import android.content.Context;
import android.content.SharedPreferences;
public class ShareHelper {
//兩個功能 保存凰锡,讀取
Context context;
public ShareHelper() {
}
public ShareHelper(Context context) {
this.context = context;
}
//保存
public void save(String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences("iot1921", Context.MODE_PRIVATE);
//創(chuàng)建一個輸入值
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
//讀取數(shù)據(jù)
public String read(String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences("iot1921", Context.MODE_PRIVATE);
return sharedPreferences.getString(key, "");
}
}
注意事項:
- 輸入值記得提交未舟,editor.commit();
- 在
MainACtivity
中記得在OnStart方法中使用ShareHelper.read傳入需要記住的值!
預(yù)覽一下成果掂为!
3. Fragment界面跳轉(zhuǎn)
這里有三個Fragment裕膀,所對應(yīng)需要三個fragment布局界面
- 首先要創(chuàng)建3個按鈕的切換xml
message.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/message_on" android:state_selected="true"/>
<item android:drawable="@drawable/message_off" android:state_selected="false"/>
</selector>
people.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/people_on" android:state_selected="true"/>
<item android:drawable="@drawable/people_off" android:state_selected="false"/>
</selector>
statue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/statue_on" android:state_selected="true"/>
<item android:drawable="@drawable/statue_off" android:state_selected="false"/>
</selector>
然后在布局界面中使用
activity_home.xml
這個界面的布局可以隨意發(fā)揮,本人做的比較簡單勇哗,見諒
- 3個
ImageView
和1個FrameLayout
-
ImageView
分別綁定點擊事件
<?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="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/basketball">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Upcoming">
</TextView>
<TextView
android:layout_width="50dp"
android:layout_marginLeft="250dp"
android:layout_height="match_parent"
android:text="+ -"
android:textSize="25sp">
</TextView>
</LinearLayout>
<FrameLayout
android:layout_marginTop="10dp"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="550dp">
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/messageImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:background="@drawable/message"
android:layout_weight="1"
android:layout_marginStart="0dp">
</ImageView>
<ImageView
android:id="@+id/peopleImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:background="@drawable/people"
android:layout_weight="1"
android:layout_marginStart="150dp"
tools:ignore="ContentDescription">
</ImageView>
<ImageView
android:id="@+id/statueImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170dp"
android:background="@drawable/statue"
android:layout_weight="1"
android:layout_marginStart="170dp"
tools:ignore="ContentDescription">
</ImageView>
</LinearLayout>
</LinearLayout>
3.1 Fragement的界面編寫
item_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:background="@drawable/backgrounds">
<ListView
android:id="@+id/messagelistView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
fragment_message.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:background="@drawable/backgrounds">
<ListView
android:id="@+id/messagelistView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
fragment_people.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000000"
android:text="這是聯(lián)系人頁面"
tools:ignore="MissingConstraints">
</TextView>
</LinearLayout>
fragment_statue.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#000000"
android:text="這是動態(tài)頁面"
tools:ignore="MissingConstraints">
</TextView>
</LinearLayout>
Fragment布局寫完就是重點如何在FrameLayout
中加載
我們還需要創(chuàng)建布局所對應(yīng)的Fragment
每個Fragment要指定加載的布局和調(diào)用的方法
onCreateView():每次創(chuàng)建昼扛、繪制該Fragment的View組件時回調(diào)該方法,F(xiàn)ragment將會顯示該方法返回的View組件欲诺。
onActivityCreated():當(dāng)Fragment所在的Activity被啟動完成后回調(diào)該方法抄谐。
在這個文件中需要實現(xiàn)
- 點擊進入聊天界面
- 可以發(fā)送消息顯示在屏幕上
我們通過List
集合中存放Map
Map
中所存放的Key就是所創(chuàng)建的Names
數(shù)組渺鹦,Value就是Images
數(shù)組
通過創(chuàng)建SimpleAdapter對象來加載fragment_message
講下這三行關(guān)鍵的代碼
//加載在哪個布局界面
messagelistView= getActivity().findViewById(R.id.messagelistView);
//適配器中的參數(shù)
/**
context 上下文
dataList 集合中存放的值
R.layout.item_listview 加載的布局界面
new String[]{"Name","Image"} String數(shù)組
new int[]{R.id.NameTextView,R.id.ImageView} int數(shù)組 獲取id
*/
simpleAdapter=new SimpleAdapter(context,dataList,R.layout.item_listview,
new String[]{"Name","Image"},
new int[]{R.id.NameTextView,R.id.ImageView});
//加載適配器
messagelistView.setAdapter(simpleAdapter);
一個獲取數(shù)據(jù)的方法getData()
//list獲取數(shù)據(jù)
public void getData(){
//遍歷數(shù)組中的數(shù)據(jù) 添加到list中
for (int i = 0; i < Names.length; i++) {
//新建map 存放數(shù)組中的數(shù)據(jù) 最后存放在list中
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name",Names[i]);
map.put("Image",Images[i]);
dataList.add(map);
}
}
MessageFragment
package com.czie.qq;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MessageFragment extends Fragment {
private Context context;
private ListView messagelistView;
List<Map<String,Object>> dataList=new ArrayList<Map<String,Object>>();
String [] Names={"一號","二號","三號","四號","五號","六號","七號"};
int [] Images={R.mipmap.kaochang,R.mipmap.kaojitui,R.mipmap.kaojichi,R.mipmap.kaoqiezi,R.mipmap.kaolajiao,R.mipmap.kaojinzhengu ,R.mipmap.kaonanguabing};
//創(chuàng)建SimpleAdapter對象
private SimpleAdapter simpleAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_message,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context=getActivity();
initView();
}
private void initView() {
messagelistView= getActivity().findViewById(R.id.messagelistView);
getData();
simpleAdapter=new SimpleAdapter(context,dataList,R.layout.item_listview,
new String[]{"Name","Image"},
new int[]{R.id.NameTextView,R.id.ImageView});
messagelistView.setAdapter(simpleAdapter);
//listView的項點擊監(jiān)聽事件
messagelistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//獲取當(dāng)前項的數(shù)據(jù)
String name=dataList.get(i).get("Name").toString();
Intent intent=new Intent(getActivity(),TalkActivity.class);
startActivity(new Intent(getActivity(), TalkActivity.class));
intent.putExtra("name",name);
Toast.makeText(context,"當(dāng)前點擊的選項名稱是"+name,Toast.LENGTH_SHORT).show();
}
});
}
//list獲取數(shù)據(jù)
public void getData(){
//遍歷數(shù)組中的數(shù)據(jù) 添加到list中
for (int i = 0; i < Names.length; i++) {
//新建map 存放數(shù)組中的數(shù)據(jù) 最后存放在list中
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name",Names[i]);
map.put("Image",Images[i]);
dataList.add(map);
}
}
}
PeopleFragment
package com.czie.qq;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
public class PeopleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_people,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
StatueFragment
package com.czie.qq;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
public class StatueFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_statue,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
最后在HomeActivity
中使用
了解什么是FragmentTransaction
使用Fragment時,可以通過用戶交互來執(zhí)行一些動作蛹含,比如增加毅厚、移除、替換等浦箱。
所有這些改變構(gòu)成一個集合吸耿,這個集合被叫做一個transaction
HomeActivity
- 監(jiān)聽
ImgaeView
的點擊事件 - 初始化
ImageView
的選中狀態(tài)為false 通過方法傳入?yún)?shù)從而判斷哪個ImageView
被點擊 將被點擊的狀態(tài)設(shè)為false -
showFragment(Fragment fragment)
中通過隱藏Fragment
,點擊ImageView
時再判斷顯示哪個Fragment
package com.czie.qq;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
@SuppressWarnings({"all"})
public class HomeActivity extends Activity implements View.OnClickListener {
private ImageView messageImageView, peopleImageView, statueImageView;
private MessageFragment messageFragment;
private PeopleFragment peopleFragment;
private StatueFragment statueFragment;
private final FragmentManager fragmentManager = getFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initView();
initFragment();
showFragment(statueFragment);
}
private void initView() {
messageImageView = findViewById(R.id.messageImageView);
peopleImageView = findViewById(R.id.peopleImageView);
statueImageView = findViewById(R.id.statueImageView);
messageImageView.setOnClickListener(this);
peopleImageView.setOnClickListener(this);
statueImageView.setOnClickListener(this);
}
private void initFragment() {
messageFragment = new MessageFragment();
peopleFragment = new PeopleFragment();
statueFragment = new StatueFragment();
//展示
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.frameLayout, messageFragment);
transaction.add(R.id.frameLayout, peopleFragment);
transaction.add(R.id.frameLayout, statueFragment);
transaction.commit();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.messageImageView:
init(messageImageView);
showFragment(messageFragment);
break;
case R.id.peopleImageView:
init(peopleImageView);
showFragment(peopleFragment);
//startActivity(new Intent(HomeActivity.this, ListViewActivity.class));
break;
case R.id.statueImageView:
init(statueImageView);
showFragment(statueFragment);
break;
default:
break;
}
}
public void init(ImageView imageView) {
messageImageView.setSelected(false);
peopleImageView.setSelected(false);
statueImageView.setSelected(false);
imageView.setSelected(true);
}
//展示指定的Fragment
//定義當(dāng)前頁面
private Fragment curFragment = new Fragment();
public void showFragment(Fragment fragment) {
//如果當(dāng)前的fragment與傳入的fragment是同一個憎茂,呢么將返回
if (curFragment.equals(fragment)) {
return;
}
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.hide(messageFragment);
transaction.hide(peopleFragment);
transaction.hide(statueFragment);
transaction.show(fragment);
transaction.commit();
}
}
注意事項
- 記得
transaction
要commit
預(yù)覽一下成果!
4. 聊天界面
item_talking.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="horizontal"
android:padding="10dp"
android:gravity="right">
<TextView
android:id="@+id/talkTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="300dp"
android:text="烤腸"
android:textSize="25sp"
android:textColor="#000000"
android:background="#cef"
android:layout_marginStart="300dp">
</TextView>
<ImageView
android:id="@+id/ImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@mipmap/kaochang">
</ImageView>
</LinearLayout>
activity_talk.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
android:text="康小莊"
android:id="@+id/NameTextView"
android:background="#44cef6">
</TextView>
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="600dp"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="bottom"
>
<EditText
android:id="@+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</EditText>
<Button
android:id="@+id/sendButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="發(fā)送">
</Button>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
TalkActivity
- 通過
Handler
來實現(xiàn)消息發(fā)出去之后 對話框置為null
- 通過
simpleAdapter
實現(xiàn)加載到哪個布局界面的id
package com.czie.qq;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TalkActivity extends Activity implements View.OnClickListener {
private EditText inputEditText;
private SimpleAdapter simpleAdapter;
List<Map<String,String>> list=new ArrayList<Map<String,String>>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talk);
Context context = this;
initView();
TextView nameTextView = findViewById(R.id.NameTextView);
Intent intent=getIntent();
String name=intent.getStringExtra("name");
nameTextView.setText(name);
}
private void initView() {
inputEditText=findViewById(R.id.inputEditText);
Button sendButton = findViewById(R.id.sendButton);
ListView listView2 = findViewById(R.id.listView2);
listView2.setDivider(null);
simpleAdapter=new SimpleAdapter(this,list,R.layout.item_talking,new String[]{"message"},new int[]{R.id.talkTextView});
listView2.setAdapter(simpleAdapter);
sendButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
sendMessage();
handler.sendEmptyMessage(0);
}
public void sendMessage(){
String message=inputEditText.getText().toString();
if (message.length()>0){
HashMap<String, String> map = new HashMap<>();
map.put("message",message);
list.add(map);
inputEditText.setText("");
}
}
Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
simpleAdapter.notifyDataSetChanged();
}
};
}
總結(jié):
- 學(xué)會了簡單的頁面跳轉(zhuǎn)功能珍语,提高思考的能力,學(xué)會如何用較少代碼實現(xiàn)功能
- 還有許多不足仍需努力
寫在最后:
- 有任何錯誤歡迎評論區(qū)指出竖幔,及時改正板乙!