2016-01-16 22 views
-1

我的好友和我正在Android Studio中编写一个简单的应用程序。当您按下按钮时,会打开一个新的活动,并显示您推送的按钮的名称,并在该文件中显示该文本。我想使用txt文件中的文字设置按钮的文本

我有生成第一组按钮(这些都是硬编码)的代码,我可以得到推动按钮的名称。我的麻烦是阅读文本文件并显示内容。文本文件中的每一行都必须是一个按钮的文本值。我不能硬编码这些单词,因为它们可以经常更改。

示例; 在主要活动中,您按下标有“圆形”的按钮,它会将您带到一个页面,该页面将名为“round”的文本文件中的所有单词列为按钮。

我希望这更清楚。

在此先感谢。

+0

能的数量按钮更改?如果答案是否定的,只是名称可以改变,也许你可以做这样的事情: http://stackoverflow.com/a/12421888/5063263而不是追加文本只是做一个数组的所有按钮,每次它循环将按钮的文本设置为从缓冲读取器获得的文本 – ivan

+0

是的,如果名称数量改变,则按钮数量改变。所以如果我现在有8个名字,那么我会有8个按钮,但明天我可能会有12个名字,所以我会有12个按钮。 – vbneil54

回答

0

假设你的文件在SD卡中,并且你的名字和你的按钮一样,这应该可以工作。在你的第一个活动中,你可以创建一个公共变量(或者你喜欢Frogatto提到的额外元素,他们真的比我更好),它存储点击的按钮名称,然后添加它+ “TXT”。

PS:我花了时间做只是因为我的好奇心的C这样的:

public class test extends Activity { 
    LinearLayout layout; 
    Button btnarr [] = new Button[50]; 
    int counter = 0,check=0; 
    protected void onCreate(Bundle savedInstanceState) { 
     counter=0; 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 

       layout = new LinearLayout(this); 
       layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
       layout.setOrientation(LinearLayout.VERTICAL); 

     File sdcard = Environment.getExternalStorageDirectory(); 

     //Get the text file 
     File file = new File(sdcard,"yourbuttonname.txt"); 

     //Read text from file 
     StringBuilder text = new StringBuilder(); 

     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line; 

      while ((line = br.readLine()) != null) { 
       text.append(line); 
       btnarr[counter] = new Button(this); 
       btnarr[counter].setText(text.toString()); 
       text = new StringBuilder(); 
       counter++; 
       check++; 
      } 
      br.close(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     doit(); 
    } 
    public void doit() 
    { 
     counter = 0; 
     while(counter < check) 
     { 
      LinearLayout row = new LinearLayout(this); 
      row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
      btnarr[counter].setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
      row.addView(btnarr[counter]); 
      layout.addView(row); 
      counter++; 
     } 
     setContentView(layout); 
    } 
} 

的test.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</LinearLayout> 

enter image description here

enter image description here

+0

谢谢。还有一个问题,你如何从资产文件夹读取文件,而不是从外部存储设备读取文件。 – vbneil54

+0

@ vbneil54你应该阅读这个答案:http://stackoverflow.com/a/9544781/5063263 – ivan

0

您应采取以下步骤:

  • 当用户点击一个按钮,创建一个Intent指向第二个活动,并附加按钮的名称,将其作为它的临时演员。

    Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
    intent.putExtra("nameOfButton", /* name of button */); 
    startActivity(inetnt); 
    
  • 在第二活动通过getIntent()方法获得到Intent的引用,并读取其额外作为按钮的名称。

    Intent intent = getIntent(); 
    String nameOfButton = intent.getStringExtra("nameOfButton"); 
    
  • 阅读相应的文本文件并相应地给布局文件充气。

+0

这听起来不错。我会在今天晚些时候尝试。谢谢。 – vbneil54

相关问题