2017-01-23 55 views
0

我正在開發一個android word搜索應用程序,並且我已經動態地創建了一個tablelayouttextviews以保存每個字母。當用戶的手指滑過字母網格時,我想更改這些textviews的背景顏色。如何更改textview背景在刷卡時的顏色?

這是表格的佈局代碼:

public void createGrid(char[][] input) { 
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl); 
    TableLayout table = (TableLayout) findViewById(R.id.mainLayout); 
    table.setTag(1); 
    Typeface font = Typeface.createFromAsset(getAssets(), "kg.ttf"); 
    int j; 



    for (int i = 0; i < input.length; i++) { 
     LinearLayout rowLayout = new LinearLayout(this); 
     rowLayout.setOrientation(LinearLayout.HORIZONTAL); 
     rowLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     final TableRow row = new TableRow(this); 
     row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 
     row.setGravity(Gravity.CENTER_HORIZONTAL); 

     for (j = 0; j < input.length; j++) { 
      final TextView text = new TextView(this); 
      Character temp = input[i][j]; 
      text.setTag(i); 
      text.setText(temp.toString()); 
      text.setPadding(20, 20, 20, 20); 
      text.setTag(i); 
      text.setTextSize(txtSize); 
      text.setTypeface(font); 
      text.setGravity(Gravity.CENTER); 
      text.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        view.setBackgroundColor(Color.parseColor("#c03768b7")); 
        ((TextView)view).setTextColor(Color.WHITE); 
       } 
      }); 
      row.addView(text); 
      row.getChildAt(j); 

     } 
     table.addView(row); 

    } 
    } 

正如你所看到的,我分配OnClickListeners每個TextViews。但我想要一個無憂無慮的突出顯示,有沒有辦法改變,以刷卡,而不是點擊每個textviews

任何意見,答案和建議非常感謝。

編輯:

我試圖做的是改變字母的顏色文本作爲文本刷卡。

+0

你想改變的文字字母的顏色與文本刷卡? – OBX

+0

@Gboy ...請檢查答案,讓我知道情況 – Athul

+0

@Superman - 是的。這就是我想要做的 – Gboy

回答

0

檢測輕掃類似下面和實施改變TextView的背景顏色的功能

 text.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 

        switch (event.getAction()) { 

         case MotionEvent.ACTION_DOWN: 
          x1 = event.getX(); 
          y1 = event.getY(); 
          t1 = System.currentTimeMillis(); 
          return true; 
         case MotionEvent.ACTION_UP: 
          x2 = event.getX(); 
          y2 = event.getY(); 
          t2 = System.currentTimeMillis(); 

          if (x1 > x2) { 
           Toast.makeText(getActivity(), "Left swipe", Toast.LENGTH_SHORT).show(); 
           // Insert your code to change the color of the textview background. 
        v.setBackgroundColor(Color.parseColor("#c03768b7")); 
          } else if (x2 > x1) { 
           Toast.makeText(getActivity(), "Right swipe", Toast.LENGTH_SHORT).show(); 
        v.setBackgroundColor(Color.parseColor("#c03768b7")); 
           // Insert your code to change the color of the textview background. 

          } 


          return true; 
        } 

        return false; 
       } 
+0

使用此txt.setBackgroundColor(Color.RED)更改backgorund; – Athul

+0

而不是像這樣添加顏色......你可以創建一個數組並使用一個函數t = o只有當你想讓背景改變多種顏色時,使用函數 – Athul

+0

中的數組索引號改變背景的顏色 – Athul