我在android中動態創建了一個textview。當我點擊textview的顏色從白色變爲橙色,但我想要的是當我點擊另一個textview時,其他已被更改爲橙色的textview將變回白色。 這是創建的TextView代碼:更改動態文本視圖中每次點擊的顏色android
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));
不錯!非常感謝!它像一個魅力!這正是我需要的:D – wizzone