UI今天給了個需求,把亂序的單詞拼成一句話,如下圖:
前期的思路是這樣的:
1.測量每個單詞的高度和寬度,計算出所有單詞排列后的高*2蒜胖,就是view 的高度
2.把文字寫上去
哈哈,這個的確就是當時的想法抛蚤,不要慫就是干~
那就來試試吧,話不多說寻狂,直接上代碼:
package com.meetime.etherflyer.page;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.meetime.etherflyer.R;
import com.meetime.etherflyer.tools.AppConfigUtil;
import com.meetime.etherflyer.tools.AppLanguage;
import com.meetime.etherflyer.tools.SPUtils;
import com.meetime.etherflyer.tools.ToastUtils;
import com.per.flowlayout.AddTagActivity;
import com.per.flowlayout.FlowLayout;
import com.per.flowlayout.FlowLayoutAdapter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MnHellpWordAcextends Activity {
private TextViewtv_remind,tv_copy_label,tv_copy_tip,tv_etherflyer_desc,tv_confirm;
? ? private FlowLayouttcy_my_label, tcy_hot_label;
? ? private FlowLayoutAdaptermMyLabelAdapter, mHotLabelAdapter;
? ? private ListHotLabelLists;
? ? private ArrayListlistArr =new ArrayList();
? ? private RelativeLayoutnext;
? ? private Stringmnemonic;
? ? private ListMyLabelLists =new ArrayList<>();
? ? private static int TAG_REQUESTCODE =0x101;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.layout_mn_hellp_word);
? ? ? ? initView();
? ? ? ? initData();
? ? }
private void initView() {
next = findViewById(R.id.next);
? ? ? ? tv_remind = findViewById(R.id.tv_remind);
? ? ? ? tcy_my_label = findViewById(R.id.tcy_my_label);
? ? ? ? tcy_hot_label = findViewById(R.id.tcy_hot_label);
? ? ? ? setLanguage();
? ? }
private void initData() {
Intent intent = getIntent();
? ? ? ? mnemonic = intent.getStringExtra("mnemonic");
? ? ? ? HotLabelLists =new ArrayList<>();
? ? ? ? String[] splits =mnemonic.split(" ");
? ? ? ? for (int i =0; i < splits.length; i++) {
listArr.add(splits[i]);
? ? ? ? }
String[] split =mnemonic.split(" ");
? ? ? ? for (int i =0; i < split.length; i++) {
HotLabelLists.add(split[i]);
? ? ? ? ? ? Collections.shuffle(HotLabelLists);
? ? ? ? }
mHotLabelAdapter =new FlowLayoutAdapter(this, HotLabelLists);
? ? ? ? tcy_hot_label.setAdapter(mHotLabelAdapter);
? ? ? ? tcy_hot_label.setItemClickListener(new TagCloudLayoutItemOnClick(1));
? ? ? ? mMyLabelAdapter =new FlowLayoutAdapter(this, MyLabelLists);
? ? ? ? tcy_my_label.setAdapter(mMyLabelAdapter);
? ? ? ? tcy_my_label.setItemClickListener(new TagCloudLayoutItemOnClick(0));
? ? ? ? String labels = String.valueOf(getIntent().getStringExtra("labels"));
? ? ? ? if (!TextUtils.isEmpty(labels) && labels.length() >0 && !labels.equals("null")) {
String[] temp = labels.split(" ");
? ? ? ? ? ? for (int i =0; i < temp.length; i++) {
if (!MyLabelLists.contains(temp[i])){
MyLabelLists.add(temp[i]);
? ? ? ? ? ? ? ? ? ? ChangeMyLabels();
? ? ? ? ? ? ? ? }
}
}
next.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View v) {
if (MyLabelLists.isEmpty()){
ToastUtils.toast("請您按順序選擇上一頁的助記詞");
? ? ? ? ? ? ? ? }else {
if (MyLabelLists.equals(listArr)){
for (String mn:listArr){
SPUtils.putStringValue("mnemonic", mn);
? ? ? ? ? ? ? ? ? ? ? ? }
Intent intent =new Intent(getApplication(),CreateWalletSAc.class);startActivity(intent);
? ? ? ? ? ? ? ? ? ? }else {
ToastUtils.toast("請您按順序選擇上一頁的助記詞");
? ? ? ? ? ? ? ? ? ? }
}
}
});
? ? }
/**
* 刷新我的標簽數據
? ? * @author dongwenlong
*/
? ? private void ChangeMyLabels() {
tv_remind.setVisibility(MyLabelLists.size() >0 ? View.GONE
? ? ? ? ? ? ? ? : View.VISIBLE);
? ? ? ? tcy_my_label.setVisibility(MyLabelLists.size() >0 ? View.VISIBLE
? ? ? ? ? ? ? ? : View.GONE);
? ? ? ? mMyLabelAdapter.notifyDataSetChanged();
? ? }
/**
* 標簽的點擊事件
*
? ? * @author dongwenlong
*/
? ? class TagCloudLayoutItemOnClickimplements FlowLayout.TagItemClickListener {
int index;
? ? ? ? public TagCloudLayoutItemOnClick(int index) {
this.index = index;
? ? ? ? }
@Override
? ? ? ? public void itemClick(int position) {
switch (index) {
case 0:
MyLabelLists.remove(MyLabelLists.get(position));
? ? ? ? ? ? ? ? ? ? ChangeMyLabels();
break;
? ? ? ? ? ? ? ? case 1:
if (MyLabelLists.size() <12) {
if (HotLabelLists.get(position).equals("自定義")) {
startActivityForResult(
new Intent(MnHellpWordAc.this,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AddTagActivity.class),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TAG_REQUESTCODE);
? ? ? ? ? ? ? ? ? ? ? ? }else {
Boolean isExits =isExist(MyLabelLists,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HotLabelLists.get(position));
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isExits) {
ToastUtils.toast("此助記詞已經添加啦");
return;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }else {
MyLabelLists.add(HotLabelLists.get(position));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ChangeMyLabels();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
}
}else {
ToastUtils.toast("最多只能添加12個助記詞哦");
? ? ? ? ? ? ? ? ? ? }
break;
? ? ? ? ? ? ? ? default:
break;
? ? ? ? ? ? }
}
}
/**
* 將數組里面的字符串遍歷一遍岁经,看是否存在相同標簽
? ? * @param str
? ? * @param compareStr
? ? * @author dongwenlong
? ? * @return
? ? */
? ? public static BooleanisExist(List str, String compareStr) {
Boolean isExist =false;//默認沒有相同標簽
? ? ? ? for (int i =0; i < str.size(); i++) {
if (compareStr.equals(str.get(i))) {
isExist =true;
? ? ? ? ? ? }
}
return isExist;
? ? }
/**
* 回傳數據
? ? *@author dongwenlong
? ? @Override*/
? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String label = data.getStringExtra("tags");
? ? ? ? MyLabelLists.add(label);
? ? ? ? ChangeMyLabels();
? ? }
private void setLanguage() {
tv_copy_label = findViewById(R.id.tv_copy_label);
? ? ? ? tv_copy_tip = findViewById(R.id.tv_copy_tip);
? ? ? ? tv_etherflyer_desc = findViewById(R.id.tv_etherflyer_desc);
? ? ? ? tv_confirm = findViewById(R.id.tv_confirm);
? ? ? ? if (AppConfigUtil.getInstance().currentLanguage == AppLanguage.CN){
tv_copy_label.setText(AppConfigUtil.getInstance().localizedString("請立即抄寫下你的備份助記詞!"));
? ? ? ? ? ? tv_copy_tip.setText(AppConfigUtil.getInstance().localizedString("助記詞可替代輸入錢包私鑰的方式導入錢包蛇券。請將它準確抄下并熟記缀壤,且存放到只有你知道的安全地方。"));
? ? ? ? ? ? tv_etherflyer_desc.setText(AppConfigUtil.getInstance().localizedString("ETHERFLYER是基于以太坊去中心化交易平臺"));
? ? ? ? ? ? tv_confirm.setText(AppConfigUtil.getInstance().localizedString("確認"));
? ? ? ? }else {
tv_copy_label.setText(AppConfigUtil.getInstance().localizedString("Please copy down your backup mnemonics immediately!"));
? ? ? ? ? ? tv_copy_tip.setText(AppConfigUtil.getInstance().localizedString("Mnemonic words can be imported into a wallet instead of entering the private key of the wallet. Please copy it down accurately and memorize it, and store it in a safe place only you know."));
? ? ? ? ? ? tv_etherflyer_desc.setText(AppConfigUtil.getInstance().localizedString("ETHERFLYER is Decentralized trading platform based on ETF"));
? ? ? ? ? ? tv_confirm.setText(AppConfigUtil.getInstance().localizedString("confirm"));
? ? ? ? }
}
}
對于這么簡單的需求來說這個代碼量稍微有點多~ 里面有很多可以優(yōu)化的地方纠亚,等有時間再來改改吧塘慕。
歡迎線下交流 wx:dwl-1591293009