1)我动态创建了许多控件。我有一个中等大小的 RelativeLayout,它的 EditText 填充了它的大部分父可见空间。其余部分被另一个视图覆盖,并且布局应该在点击时向上滑动。问题是当用户点击 EditText 时布局没有收到 onClick 事件。我不能使用 TextView,因为稍后我需要用户在其中输入一些文本。我试过这段代码,没有成功
private void addTopView() {
RelativeLayout midView = new RelativeLayout(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.wallet_top);
imageView.setBackgroundColor(0xffff0000);
RelativeLayout.LayoutParams lp = new LayoutParams(scale(320), scale(heightTop));
midView.addView(imageView, lp);
RelativeLayout topCard = populateTPasses ? addTpass(0) : addCard(0);
topCard.setOnClickListener(topCardClick);
midView.addView(topCard);
lp = new LayoutParams(scale(320), scale(cardTopOffsetY + cardHeight));
lp.topMargin = scale(upperOffset);
walletScrollView.addView(midView, lp);
}
private RelativeLayout addCard(int indexVal) {
String cardName = "Add Credit Card";
String cardNum = "Max 16 digits";
String cardCVC = "Max 6 digits";
boolean switchStatus = NO;
String cardExpiry = dateFormatter.format(new Date());
if(indexVal < creditcards.size())
{
cardName = creditcards.get(indexVal).name;
cardNum = String.format("........%s", creditcards.get(indexVal).display);
cardCVC = "NOT STORED";
cardExpiry = creditcards.get(indexVal).expiry;
switchStatus = creditcards.get(indexVal).isDefault;
}
LayoutParams lp = new LayoutParams(scale(cardWidth), scale(cardHeight));
lp.leftMargin = scale(cardTopOffsetX);
lp.topMargin = scale(cardTopOffsetY);
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(lp);
layout.setTag(indexVal);
addCardElements(layout, cardName, cardNum, cardExpiry, cardCVC, switchStatus);
return layout;
}
private ImageView addCardElements(RelativeLayout whichCard, String cardName, String cardNumber, String cardExpiry, String cardCVC, boolean defaultStatus) {
whichCard.setBackgroundResource(R.drawable.credit_card_tpay);
//Add card name
EditText cardNameField = addCardName(cardName);
whichCard.addView(cardNameField);
return null;
}
private EditText addCardName(String cName) {
EditText cardName = new EditText(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(scale(cardWidth - 90), LayoutParams.WRAP_CONTENT);
params.leftMargin = scale(32 + 50);
params.topMargin = scale(15);
cardName.setBackgroundColor(Color.TRANSPARENT);
cardName.setLayoutParams(params );
cardName.setTextColor(Color.WHITE);
if(isLast == NO || (_selectedIndex >= creditcards.size() && creditcards.size() != 0))
cardName.setText(cName);
else
{
cardName.setHint(cName);
}
isLast = NO;
cardName.setTag(kTag_CardName);
if(_selectedIndex >= creditcards.size())
cardName.setEnabled(YES);
else {
cardName.setEnabled(NO);
cardName.setFocusable(NO);
cardName.setFocusableInTouchMode(NO);
cardName.setClickable(NO);
cardName.setBackgroundColor(Color.YELLOW);
}
cardName.setGravity(5);
return cardName;
}
2)这个解决了。 当我的布局在 TranslateAnimation 的帮助下向上滑动时,我必须将其从其父级中删除并放到另一个 ViewGroup 上,否则所有用户输入都会被破坏。然后它以类似的父级变化滑回。在此过程中,我将布局的 OnClickListener 设置为 null 并返回给我的侦听器。问题是在那之后监听器永远不会触发并且布局只能滑动一次。我想知道这可能是什么原因造成的。