我在动态添加按钮和设置onClickListener时遇到问题。这就是我现在所拥有的:
List<Button> bList = new ArrayList<Button>();
for(int i = 0; i < 3; i++) {
q = i;
bList.add(new Button(this.activity));
bList.get(i).setText(Integer.toString(q));
bList.get(i).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
statsButton.setText(Integer.toString(q));
}
});
upgradeLayout.addView(bList.get(i));
}
除了听众不工作外,它工作正常。当我点击按钮时没有任何反应,但按钮创建得很好。
答案:这是导致问题的与此无关的愚蠢行为。道歉。答案 0 :(得分:0)
试试这个......
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dynamically adding buttons and setting Listeners" />
<LinearLayout
android:id="@+id/btn_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
2的 MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addbtnDynamically();
}
//Method to generate button dynamically
private void addbtnDynamically(){
try{
LinearLayout layout = (LinearLayout) findViewById(R.id.btn_layout);
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 3; j++ ){
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
button.setText("Button " + (j + 1 + (i * 3)));
button.setTextColor(Color.WHITE);
button.setId(j + 1 + (i * 3));
button.setOnClickListener(onClickListener);
row.addView(button);
}
layout.addView(row);
}
}catch(Exception b){b.printStackTrace();}
}
//Method for click event
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case 1:
showToast("Button 1 clicked!");
break;
case 2:
showToast("Button 2 clicked!");
break;
case 3:
showToast("Button 3 clicked!");
break;
case 4:
showToast("Button 4 clicked!");
break;
// .......
}
}
};
//Method to message
private void showToast(String content){
Toast.makeText(MainActivity.this,""+content,Toast.LENGTH_SHORT).show();
}
}
3 <强>结果强>
你应该得到像这样的输出......
快乐的编码......
答案 1 :(得分:0)
试试这个,
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.addbuttondynamic.AddButtonDynamic" >
</LinearLayout>
Main.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AddButtonDynamic extends Activity {
LinearLayout upgradeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_button_dynamic);
upgradeLayout=(LinearLayout)findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for(int k=0; k<3; k++)
{
LinearLayout ll = new LinearLayout(this);
ll.setId(k+1);
ll.setLayoutParams(params);
for(int i=0;i<3;i++) {
Button btn = new Button(this);
btn.setId(i+1);
btn.setText("Button"+(i+1));
btn.setLayoutParams(lprams);
final int index = i;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("TAG", "The index is" + index);
Toast.makeText(getApplicationContext(), "Click "+index, Toast.LENGTH_LONG).show();
}
});
ll.addView(btn);
}
upgradeLayout.addView(ll);
}
}
}
很高兴为您提供帮助。