2017-01-23 99 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