2017-08-10 58 views
0

我在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)); 

回答

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)); 
      } 
     }); 
    } 
} 
+0

不錯!非常感謝!它像一個魅力!這正是我需要的:D – wizzone

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; 
    } 
}); 
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); 

現在你必須實現這樣

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.1: 
      //code for click textview. 
      break; 
    } 
} 

快樂編碼的onclick方法。

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),則設置爲白色。

相關問題