簡體   English   中英

我正在嘗試將歌曲和歌手的名字分開

[英]I'm trying to separate the name of the song and the artist

我正在嘗試將歌曲的名稱和歌手分開,我將內容文本視圖命名為“ SongName_Artist”,如何將SongsName和Artist分成2個textview

獲取布局(您將需要一個ID):

mLayout = findViewById(R.id.rootLayout);

查找您的內容文字:

contentTextView = findViewById(R.id.contentTextView);
String contentText = contentTextView.getText().toString();

為歌曲和歌手創建TextView

TextView songTextView = new TextView(this);
TextView artistTextView = new TextView(this);
songTextView.setText(contentText.split("_")[0]);
artistTextView.setText(contentText.split("_")[1]);

將它們添加到布局:

mLayout.addView(songTextView);
mLayout.addView(artistTextView);

Java具有內置的split函數,在這種情況下可以使用。 以下是Java代碼的外觀:

    String whole = "Song_Artist";
    String[] pieces = whole.split("_");
    String song = pieces[0]; //everything before the _
    String artist = pieces[1]; //everything after the _

現在,您已經分解了字符串,可以繼續使用setText來根據需要設置textViews。

您可以使用string.split()方法來分隔字符串。

 String str = "soniye_falak";
   String[] parts = string.split("_");
    String song = parts[0]; // soniye
    String artist = parts[1]  //falak

split()方法用於將字符串拆分為子字符串數組,並返回新數組。 如果將空字符串(“”)用作分隔符,則該字符串將在每個字符之間分割。 注意:split()方法不會更改原始字符串。

您可以使用字符串Eg(“ your_string”)。split(“ _”)的拆分函數並收集返回值代碼,請執行以下操作:

String song_artist = "S_A";
System.out.println(song_artist.split("_"));
String[] temp = song_artist.split("_");
    for(int i=0; i<temp.length;i++) {
        System.out.println(temp[i]);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM