我有许多动态创建的按钮,现在我想根据某些条件更改某些按钮的背景。
Button btn;
Button btn2;
for(int i=0;i<20;i = i+2) {
LinearLayout linearLayout = new LinearLayout(mContext);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
// LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
// LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,100);
params.leftMargin = 10;
params.rightMargin = 10;
params.topMargin = 10;
params.bottomMargin = 10;
btn = new Button(this);
btn.setId(i+1);
btn.setText( ""+(i+1));
btn.setBackground(image);
btn.setTag(i+1);
buttons.add(btn);
ids.add(i+1);
final int index = i;
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("TAG", "The index is" + index);
String cmd = "";
if((index+1) >=10) {
cmd = createCmd("" + (index + 1));
}else {
cmd = createCmd("0" + (index + 1));
}
}
});
final int index2 = i+1;
btn2 = new Button(this);
btn2.setId(i+2);
btn2.setText(""+ (i+2));
btn2.setTag(i+2);
buttons.add(btn2);
ids.add(i+2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", "The index is" + index2);
String cmd2 = "";
if((index2+1) >=10){
cmd2 = createCmd(""+(index2+1));
}else {
cmd2 = createCmd("0"+(index2+1));
}
}
});
Log.e(TAG ,"btn.getTag() : "+btn.getTag());
Log.e(TAG ,"btn.getTag() : "+btn2.getTag());
btn2.setBackground(image);
btn.setTextColor( TempSharedPreference.getKeyTextColor(mContext)) ;
btn2.setTextColor( TempSharedPreference.getKeyTextColor(mContext)) ;
linearLayout.addView(btn,params);
linearLayout.addView(btn2,params);
llDynamicButtons.addView(linearLayout);
}
答案 0 :(得分:2)
您可以随时使用视图组执行此类操作。
int count = yourLinearLayout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
v = layout.getChildAt(i);
if(v instanceof Button && v.getTag() == 1/2/3){
v.setBackgroundColor(Color.parseColor("#ff2233"));
}
}
答案 1 :(得分:0)
亲爱的,亲爱的,这就是在布局中动态添加按钮的设置和获取ID的解决方案。
试试这个。 希望这会对你有所帮助。
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Button;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class GetIdDynamicallyAddedContentActivity extends AppCompatActivity {
ArrayList<Button> buttons;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params1);
buttons = new ArrayList<>();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 10; i++) {
Button b = new Button(this);
b.setId(i + 1);
b.setTag(i + 1);
b.setText("" + (i + 1));
b.setLayoutParams(params);
buttons.add(b);
}
for (int j = 0; j < buttons.size(); j++) {
layout.addView(buttons.get(j));
}
setContentView(layout);
printIds();
}
public void printIds() {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() % 2 == 0) {
buttons.get(i).setBackgroundColor(Color.RED);
} else {
buttons.get(i).setBackgroundColor(Color.BLUE);
}
}
}
}