如何制作名称为android的首字母缩写为两个字符的缩略图



我想在我的图像视图中用两个单词缩略首字母缩写,比如"Peter Parker",但在运行代码时只能得到一个单词"p"如何在我的代码空格后得到第二个单词。

holder.imgName?.text=teamData[position].userImage.substring(0,1)

您可以通过以下功能方式完成:

val peterParker = "Peter Parker"
val initials = peterParker
.split(' ')
.mapNotNull { it.firstOrNull()?.toString() }                     
.reduce { acc, s -> acc + s }
println(initials) //PP

这将涵盖一个人的名字包含两个以上单词的情况。

我做了一些技巧&用按钮lol实现了这个化身;p

创建profile_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="@color/colorWhite"/>
<corners
android:radius="500dp"/>
</shape>

main_activity.xml

<?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"
android:background="#4300313A"
tools:context=".MainActivity">
<Button
android:onClick="clicked"
android:id="@+id/avatar"
android:clickable="false"
android:focusable="false"
android:textColor="@color/colorPrimary"
android:textSize="65sp"
android:focusableInTouchMode="false"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/profile_bg"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<EditText
android:id="@+id/edtname"
android:layout_below="@+id/avatar"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:hint="Enter your name"/>
<Button
android:onClick="clicked"
android:textColor="@color/colorBackground"
android:text="Submit Name"
android:textStyle="bold"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/edtname"
android:layout_marginTop="50dp"/>
</RelativeLayout>

然后在MainActivity.java中(使用字符串生成器在if条件中拆分字符串并获取每个单词的第一个字母~名称(

public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edtname);
button = (Button) findViewById(R.id.avatar);
}
public void clicked(View view) {
String str = editText.getText().toString();
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
//First name
if (strArray.length > 0){
builder.append(strArray[0], 0, 1);
}
//Middle name
if (strArray.length > 1){
builder.append(strArray[1], 0, 1);
}
//Surname
if (strArray.length > 2){
builder.append(strArray[2], 0, 1);
}
button.setText(builder.toString());
}
}

嗨,您可以使用以下方式

String str = "FirstWord SecondWOrd";
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
if (strArray.length > 0)
builder.append(strArray[0], 0, 1);
if (strArray.length > 1)
builder.append(strArray[1], 0, 1);
Log.d("New Text" , builder.toString());

这看起来就像你使用子串只抓取从位置0到位置1的字母,这是Petter 中的p

holder.imgName?.text=teamData[position].userImage  

.substring(0,1(

如果你想抓住Petter Parker这个词,你有几个选择
•索引;子字符串-查找字符串的位置并获取后面的子文本
•子字符串-基于参数的字符串子文本

如果你打算在任何阶段更改文本长度,你需要找到单词的开头(int start = yourString.indexOf("Petter")(
和词尾(int end = yourString.indexOf(" "((

IndexOf将返回查询中第一个字母的位置-您的情况是Petter中的p-所以开始+"Petter".length((

这是一个条形码价格检查器应用程序的例子,我正在上工作

// STRING  FORMAT 00000899999:0.99   
String currentLine = "00000899999:0.99";
int breakPoint = currentLine.indexOf(":");
int end = currentLine.length();
int start = breakPoint + 1;
String Price = currentLine.substring(start,end);

价格将在(:(带+1后开始,或包含(:(不带+1并以行长度结束。

我在Kotlin中编写了一个扩展函数来获取名称的首字母缩写。您可以使用自定义视图,并使用您想要的绘制文本和剪辑形状作为头像视图。

val initials = "Vikas Patidar".asInitials()
fun String.asInitials(limit: Int = 2): String {
val buffer = StringBuffer()
trim().split(" ").filter {
it.isNotEmpty()
}.joinTo(
buffer = buffer,
limit = limit,
separator = "",
truncated = "",
) { s ->
s.first().uppercase()
}
return buffer.toString()
}

最新更新