2014-01-18 115 views
-2

我想在屏幕上显示textview的多个标签,当我将触摸屏幕。现在我正在使用我在xml中创建的标签(Textview),但是不可能在xml中创建多个标签。有什么办法可以动态创建多个标签吗? 我的代码:如何在Android屏幕上点击屏幕时显示多个标签(Textview)?

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

@SpringBreaker我是初学者。我没有那么多的知识。如果你有我的问题的答案,然后帮助我。 –

+0

你想要显示什么标签? –

+0

这是一个texview .. –

回答

0

我假设你想在触摸的地方screen.Use的下面的代码显示在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; 

    } 

} 

希望它有帮助。

+0

谢谢你的帮助.. –