当我触摸屏幕时,我想在屏幕上显示多个textview标签。现在我使用的是我在xml中创建的标签(Textview),但是不能在xml中创建多个标签。有没有办法动态创建多个标签? 我的代码:
public boolean onTouch(View arg0, MotionEvent event)
{
show tag();
}
private void showTag() {
if(tagFlag==0)
{
AbsoluteLayout.LayoutParams params = ((AbsoluteLayout.LayoutParams) _tagLiley.getLayoutParams());
params.x = (int) tagX;
params.y = (int) tagY;
_tagLiley.setLayoutParams(params);
_tagLiley.setVisibility(View.VISIBLE);
tag1=_categorySearchET.getText().toString().trim();
_tagName.setText(tag1);
_tagName.setTextSize(8);
tagFlag=1;
}
else if (tagFlag==1) {
AbsoluteLayout.LayoutParams params = ((AbsoluteLayout.LayoutParams) _tagLiley2.getLayoutParams());
params.x = (int) tagX;
params.y = (int) tagY;
_tagLiley2.setLayoutParams(params);
_tagLiley2.setVisibility(View.VISIBLE);
tag2=_categorySearchET.getText().toString().trim();
_tagName2.setText(tag2);
_tagName2.setTextSize(8);
tagFlag=2;
}
else if (tagFlag==2) {
AbsoluteLayout.LayoutParams params = ((AbsoluteLayout.LayoutParams) _tagLiley3.getLayoutParams());
params.x = (int) tagX;
params.y = (int) tagY;
_tagLiley3.setLayoutParams(params);
_tagLiley3.setVisibility(View.VISIBLE);
tag3=_categorySearchET.getText().toString().trim();
_tagName3.setText(tag3);
_tagName3.setTextSize(8);
}
}
答案 0 :(得分:0)
我假设你想要在屏幕的触摸位置TextView
显示文字。使用以下代码,
<强> activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Relative"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<强> MainActivity.java 强>
public class MainActivity extends Activity implements OnTouchListener {
RelativeLayout rel;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
rel=(RelativeLayout)findViewById(R.id.Relative);
rel.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e) {
int eventaction = e.getAction();
int x,y;
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:{ // touch on the screen event
x = (int)e.getX();
y = (int)e.getY();
}
case MotionEvent.ACTION_MOVE:{ // move event
x = (int)e.getX();
y = (int)e.getY();
}
case MotionEvent.ACTION_UP:{ // finger up event
x = (int)e.getX();
y = (int)e.getY();
text=new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(100, 100);
lp.topMargin=y;
lp.leftMargin=x;
text.setTextColor(Color.BLACK);
text.setTextSize(20);
text.setText("("+x+","+y+")");
rel.addView(text, lp);
break;
}
}
return false;
}
}
希望它有所帮助。