[英]Android onAccessibilityEvent get X and Y position of TextView
[英]Setting TextView on position x,y
您好,我想在用户触摸按钮时在TextView上显示字符串。 但是我希望此TextView显示在用户触摸屏幕的同一位置。 目前,我有以下代码,但无法解决:
private void set(Integer name, Float x, Float y, Typeface font) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(10, 1, 10, 1);
RelativeLayout nlap = new RelativeLayout(this);
nlap.setLayoutParams(layoutParams);
TextView tv1 = new TextView(this);
tv1.setX(x);
tv1.setY(y);
tv1.setText("+ " + String.valueOf(name));
tv1.setTypeface(font);
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(20);
nlap.addView(tv1);
}
在OnTouchListener中:
int action = motionEvent.getAction();
int x = (int)motionEvent.getX();
int y = (int)motionEvent.getY();
set(multiplicator, Float.intBitsToFloat(x), Float.intBitsToFloat(y), font);
我希望你能帮助我。 谢谢!
向该活动声明textView全局
TextView tv;
然后在oncreate()
方法中
tv=(TextView) findViewById(R.id.your_xml_textview);
tv.setText("your desired text");
tv.setVisibility(View.INVISIBLE);
或者,您可以在XML中简单地使用android:visibility="invisible"
现在ontouch
屏幕,我们得到坐标,并通过以下代码将textView放在该点上
@Override
public boolean onTouchEvent(MotionEvent event) {
int corx = (int)event.getX();
int cory = (int)event.getY();
tv.setX(corx);
tv.setY(cory);
tv.setVisibility(View.VISIBLE);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.