我在android中动态创建了一个textview。当我点击文本视图时,颜色从白色变为橙色,但我想要的是当我点击另一个文本视图时,另一个已更改为橙色的文本视图将变回白色。 这是创建textview的代码:
for (int i = 1; i <= n; i++) {
final TextView mPageNumber = new TextView(getActivity());
mPageNumber.setText("" + i);
mPageNumber.setId(Integer.parseInt(String.valueOf(i)));
mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
mPageNumber.setPadding(60,30,60,30);
final int id_ = mPageNumber.getId();
LinearLayout layout = (LinearLayout) getView().findViewById(R.id.pagination);
layout.setBackgroundResource(R.color.colorPrimary);
layout.addView(mPageNumber);
OnClickListener
mPageNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (toastMessage!= null) {
toastMessage.cancel();
}
toastMessage = Toast.makeText(getActivity().getApplicationContext(), "Button with id =" + id_ +
" is clicked",Toast.LENGTH_SHORT);
current = id_;
toastMessage.show(); mPageNumber.setTextColor(getResources().getColor(R.color.colorOrange));
答案 0 :(得分:0)
你可以像这样使用:〜
mPageNumber.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
textView.setTextColor(Color.RED);
break;
case MotionEvent.ACTION_UP:
textView.setTextColor(Color.BLUE);
break;
}
return false;
}
});
答案 1 :(得分:0)
您可以遍历布局中的所有子项,并将颜色设置为白色,然后将所选颜色设置为橙色,如下例所示。
LinearLayout layout;
private void setAllTextColorAsWhite() {
if(layout == null) {
return;
}
int childCount = layout.getChildCount();
for (int i = 0; i < childCount; i++) {
TextView textView = (TextView) layout.getChildAt(i);
textView.setTextColor(getResources().getColor(R.color.white));
}
}
public void setTextViews() {
layout = (LinearLayout) getView().findViewById(R.id.pagination);
layout.removeAllViews();
for (int i = 1; i <= n; i++) {
final TextView mPageNumber = new TextView(getActivity());
mPageNumber.setText("" + i);
mPageNumber.setId(Integer.parseInt(String.valueOf(i)));
mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
mPageNumber.setPadding(60, 30, 60, 30);
final int id_ = mPageNumber.getId();
layout.setBackgroundResource(R.color.colorPrimary);
layout.addView(mPageNumber);
mPageNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (toastMessage!= null) {
toastMessage.cancel();
}
toastMessage = Toast.makeText(getActivity().getApplicationContext(), "Button with id =" + id_ +
" is clicked",Toast.LENGTH_SHORT);
current = id_;
toastMessage.show();
setAllTextColorAsWhite();
mPageNumber.setTextColor(getResources().getColor(R.color.colorOrange));
}
});
}
}
答案 2 :(得分:0)
你必须这样做
for (int i = 1; i <= n; i++) {
final TextView mPageNumber = new TextView(getActivity());
mPageNumber.setText("" + i);
mPageNumber.setId(Integer.parseInt(String.valueOf(i)));
mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
mPageNumber.setPadding(60,30,60,30);
mPageNumber.setOnClickListener(this);
final int id_ = mPageNumber.getId();
LinearLayout layout = (LinearLayout) getView().findViewById(R.id.pagination);
layout.setBackgroundResource(R.color.colorPrimary);
layout.addView(mPageNumber);
现在你必须像这样实现onclick方法
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.1:
//code for click textview.
break;
}
}
快乐的编码。
答案 3 :(得分:0)
最简单的方法是将TextView引用保存到数组中,如下所示:
final TextView[] mPageNumbers;
for (int i = 1; i <= n; i++) {
mPageNumbers[i] = new TextView(getActivity());
mPageNumber[i].setText("" + i);
.
.
.
for (int i=0; i<mPageNumbers.length; i++) {
if (view.getId() == mPageNumbers[i].getId()) {
mPageNumber.setTextColor(getResources().getColor(R.color.colorOrange));
} else {
mPageNumber.setTextColor(getResources().getColor(R.color.colorWhite));
}
}
最后,你循环TextView引用,然后手动设置颜色,如果id相同,设置为橙色,如果不同(意味着其他textViews),设置为白色。