gpt4 book ai didi

android - 我如何在 android 的 TextView 中添加查看更多/查看更少按钮

转载 作者:行者123 更新时间:2023-11-30 01:51:44 25 4
gpt4 key购买 nike

我正在创建一个 android 应用程序,如果行数超过 4 或 5,我需要创建一个带有查看更多/查看更少按钮的 TextView 。我需要做什么才能在我的 TextView 中实现它

最佳答案

我以前没有这样做过,但这样的事情应该有效:让您的类实现 TextWatcher 接口(interface)。

当您创建 TextView 时,添加 textView.addTextChangedListener(this);

然后添加:

public void afterTextChanged(Editable s) {
if(textView.getLineCount() >= 4) {
ToggleButton showMoreToggle = (ToggleButton) findViewById(R.id.showMoreToggle);
showMoreToggle.setVisibility(View.VISIBLE);
}
}
public void onTextChanged(CharSequence s,int start, int before, int count) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

然后在 XML 中创建一个 ToggleButton 并添加 android:click="onClick"android:visibility="gone"。然后在您的 Activity 代码中输入:

public void onClick(View v) {
ToggleButton tb = (ToggleButton) v;
textView.setMaxLines(tb.isChecked() ? 10 : 4);
}

关于android - 我如何在 android 的 TextView 中添加查看更多/查看更少按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32939030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com