我正在开发一个Android单词搜索应用程序,我已动态创建一个tablelayout
,其上带有textviews
来保存每个字母。当用户的手指在字母网格上滑动时,我想更改那些textviews
的背景颜色。
这是表格布局的代码:
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 :(得分: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;
}