简体   繁体   English

可以在Android Studio中自动填充代码语句

[英]Possible to auto fill code statements in android studio

I'm currently creating a soundboard app. 我目前正在创建一个音板应用程序。 I have about 100+ sound files to import. 我大约有100多个声音文件要导入。

I have code lines (android:onClick="song1") and (MediaPlayer mysound1). 我有代码行(android:onClick =“ song1”)和(MediaPlayer mysound1)。

Just wondering if there is a way to copy+paste these lines and have android studio auto change the line to "song2" and "song3" all the way to "song100"? 只是想知道是否有一种方法可以复制并粘贴这些行,并使android studio自动将行更改为“ song2”,而将“ song3”更改为“ song100”吗? Same goes for the "mysound1" all the way to "mysound100". “ mysound1”一直到“ mysound100”都一样。 I hope I do not have to do it manually :( 我希望我不必手动进行:(

Thank you! 谢谢!

The approach you're using seems like a brute force method. 您使用的方法似乎是蛮力方法。 There are much more elegant ways to approach this. 有很多优雅的方法可以解决此问题。 For one, I would recommend creating dynamic Android components programmatically instead of in XML. 首先,我建议以编程方式而不是XML方式创建动态Android组件。 Then you could hold all of your elements in a List or a Map, and you could tie them to a generic onClick listener. 然后,您可以将所有元素保存在列表或地图中,并将它们绑定到通用的onClick侦听器。 I'd recommend taking a look around online to figure out how to do some of these things. 我建议您在网上四处看看,以了解如何做这些事情。

But, if you would like to stick with you original method, I don't believe that Android has a way to auto number your click events. 但是,如果您想坚持使用原始方法,那么我不认为Android可以自动为您的点击事件编号。 You could, however, write code to write your code. 但是,您可以编写代码来编写代码。 Here's a simple example using Java - since you're writing an Android app (utilizing the Eclipse console to print out to): 这是一个使用Java的简单示例-因为您正在编写一个Android应用程序(利用Eclipse控制台进行打印):

public class WriteMyCode {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer();
    for (int i = 1; i <= 100; i++) {
        sb.append("<Button android:id=\"@+id/mybutton\"\n");
        sb.append("  android:layout_width=\"wrap_content\"\n");
        sb.append("  android:layout_height=\"wrap_content\"\n");
        sb.append("  android:text=\"Click me!\"\n");
        sb.append("  android:onClick=\"" + "song" +  i + "\" />\n\n");
    }
    System.out.println(sb.toString());
  }
}

You can then copy the output from the Eclipse console and paste it into your Android XML resource file. 然后,您可以从Eclipse控制台复制输出并将其粘贴到您的Android XML资源文件中。

I went to use Microsoft Excel to auto fill the numbers all the way to 100. Then CONCATENATE the ; 我使用Microsoft Excel将数字自动填充到100。然后CONCATENATE; to the end of "Mediaplayer mysound1". 到“ Mediaplayer mysound1”的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM