好吧,今天我第二次得到了我需要的东西(恢复了它),只需要弄清楚下一步该做什么,但这是我的代码。
我的布局主 .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</RelativeLayout>
我的活动。基于在网上找到的 ANDROID 中的 DYNAMIC BUTTON。http://alexanderoorayil.blogspot.co.uk/2012/05/dynamic-button-in-android.html
package com.example.dynamic_buttons;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
public class MyActivity extends Activity {
Button b;
ScrollView scrollview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//String Manuf;
scrollview = new ScrollView(this);
LinearLayout linearlayout = new LinearLayout(this);
linearlayout.setOrientation(LinearLayout.VERTICAL);
scrollview.addView(linearlayout);
for(int i = 0; i<28;i++)
{
LinearLayout linear1 = new LinearLayout(this);
linear1.setOrientation(LinearLayout.HORIZONTAL);
linearlayout.addView(linear1);
b = new Button(this);
int id = getResources().getIdentifier("Manuf"+i, "string", getPackageName());
String Manuf = getResources().getString(id);
b.setText(Manuf);
b.setId(i);
b.setTextSize(30);
b.setPadding(0,0,0,0);
// b.setTypeface(Typeface.SERIF,Typeface.ITALIC);
b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
linear1.addView(b);
/* CheckBox checkbox = new CheckBox(this);
checkbox.setId(i);
checkbox.setText("Wow..");
linear1.addView(checkbox);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Checked.."+ arg0.getId(), Toast.LENGTH_SHORT).show();
}
});
*/
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Yipee.."+ v.getId(), Toast.LENGTH_SHORT).show();
}
});
}
this.setContentView(scrollview);
}
}
我的 strings.xml 文件中的字符串以 Manfu0 -28 开头。
如果没有您的帮助和 cmets,我无法做到这一点,希望我能学习如何使用,因为现在我需要为某些被按下的按钮 id 创建一些操作。
谢谢