假设你的文件在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>
能的数量按钮更改?如果答案是否定的,只是名称可以改变,也许你可以做这样的事情: http://stackoverflow.com/a/12421888/5063263而不是追加文本只是做一个数组的所有按钮,每次它循环将按钮的文本设置为从缓冲读取器获得的文本 – ivan
是的,如果名称数量改变,则按钮数量改变。所以如果我现在有8个名字,那么我会有8个按钮,但明天我可能会有12个名字,所以我会有12个按钮。 – vbneil54