我正在以编程方式将按钮添加到我的布局中,并在每个按钮之间使用dividerVertical视图。
你知道当你点击一个按钮时,按钮会变暗,片刻显示它已被按下。因为我在彼此之下添加更多按钮,只有底部的按钮会显示为按下。即使我按下其中一个按钮,底部按钮仍然会显示为按下的按钮。
在OnClickListener中然后会显示我单击的正确按钮。
作为一个例子:当按下" firstperson"时,"第三人"将被显示为按下(它会变暗一会儿)。但在onclicklistener中将检索firstpersons文本。有谁知道我怎么能解决这个问题?
我的mainLayout文件,其中包含按钮:
public class MainLayout
{
public static Button getButtonWithImage(int id, Context context,Drawable imageView, Drawable theme, int below, int color, String born, String name)
{
LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if(below != -1) paramsImage.addRule(RelativeLayout.BELOW, below);
Button b = new Button(context);
b.setId(id);
b.setLayoutParams(paramsImage);
b.setText(name + "\n" + born);
Rect t = imageView.getBounds();
imageView.setBounds( 0, 0, 96, 96 );
b.setCompoundDrawables(null, null, imageView,null);
b.setGravity(Gravity.LEFT);
b.setBackground(theme);
b.setTextColor(color);
return b;
}
public static View layingView(int id, int below, Drawable attribute, Context context )
{
LayoutParams paramsViewThird = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 3);
paramsViewThird.addRule(RelativeLayout.BELOW, below);
View v3 = new View(context);
v3.setId(id);
v3.setBackground(attribute);
v3.setLayoutParams(paramsViewThird);
return v3;
}
}
制作布局的方法:
public void visAlleKunder(RelativeLayout rel)
{
int[] attrsV = new int[] {android.R.attr.dividerVertical};
TypedArray td = obtainStyledAttributes(attrsV);
Drawable attribute = td.getDrawable(0);
td.recycle();
int[] attrs = new int[] {android.R.attr.selectableItemBackground};
TypedArray ta = obtainStyledAttributes(attrs);
Drawable drawableFromTheme = ta.getDrawable(0);
ta.recycle();
int color = (getResources().getColor(R.color.black));
Button button = MainLayout.getButtonWithImage(01, getApplicationContext(), (getResources().getDrawable(R.drawable.ic_action_edit)), drawableFromTheme, -1, color, "06.11.1995", "firstperson");
View dividerView = MainLayout.layingView(02, 01, attribute, getApplicationContext());
button.setOnClickListener(this);
rel.addView(button);
rel.addView(dividerView);
Button button2 = MainLayout.getButtonWithImage(11, getApplicationContext(), (getResources().getDrawable(R.drawable.ic_action_edit)), drawableFromTheme, 02, color, "02.02.1987", "secondperson");
View dividerView2 = MainLayout.layingView(12, 11, attribute, getApplicationContext());
button2.setOnClickListener(this);
rel.addView(button2);
rel.addView(dividerView2);
Button button3 = MainLayout.getButtonWithImage(31, getApplicationContext(), (getResources().getDrawable(R.drawable.ic_action_edit)), drawableFromTheme, 12, color, "06.02.1980", "thirdperson");
View dividerView3 = MainLayout.layingView(32, 31, attribute, getApplicationContext());
button3.setOnClickListener(this);
rel.addView(button3);
rel.addView(dividerView3);
}
@Override
public void onClick(View view)
{
Button b = (Button) view;
String tekst = b.getText().toString();
System.out.println("Buttons Choosen text: " + tekst);
}
布局如何:
这成了一个很长的帖子,但我会很感激我可能做错的任何答案:)
答案 0 :(得分:3)
注意你如何设置button2的监听器而不是button3:
button2.setOnClickListener(this);
将其更改为
button3.setOnClickListener(this);