Java 动态文本视图没有完美的单击侦听器或触摸侦听器

Java 动态文本视图没有完美的单击侦听器或触摸侦听器,java,android,Java,Android,这是我根据句子中的单词实现文本视图数量的方法。每个单词都有单击事件或触摸事件 static ArrayList<TextView> sentence(String[] arr, LinearLayout linelay_wordIn1) { if (linelay_wordIn1.getChildCount() > 0) linelay_wordIn1.removeAllViews(); if (allTextView != nu

这是我根据句子中的单词实现文本视图数量的方法。每个单词都有单击事件或触摸事件

static ArrayList<TextView> sentence(String[] arr,
        LinearLayout linelay_wordIn1) {
    if (linelay_wordIn1.getChildCount() > 0)
        linelay_wordIn1.removeAllViews();
    if (allTextView != null) {
        allTextView.remove(txt);
        allTextView.clear();
        System.out.println("hello remove all textview here");
    } else {
        System.out.println("hello all textview array is null here");
    }

    String str1 = "";

    for (int i = 0; i < arr.length; i++) {
        str1 = str1 + arr[i].toString();
        System.out.println(" senctence separte in word " + arr[i]
            + " words" + arr.length);
    }
    /* listview for getting textview */

    System.out.println("sentence " + str1.toString() + "str1 length :: "
        + str1.length());
    txt = new TextView[arr.length];
    LinearLayout.LayoutParams lp;
    lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER;
   // lp.setMarginStart(arr.length);
    for (int j = 0; j < arr.length; j++) {
        txt[j] = new TextView(contextG);
        txt[j].setId(j);
        txt[j].setBackgroundResource(Color.TRANSPARENT);
        txt[j].setTextSize(60);
        txt[j].setTypeface(
            Typeface.createFromAsset(contextG.getAssets(), "TIMES.TTF"),
            Typeface.BOLD);
        txt[j].setText(arr[j]);
        txt[j].setLayoutParams(lp);
        txt[j].setTextColor(Color.BLACK);
        txt[j].setOnTouchListener(myListner);
        System.out.println("txt[j]" + j + "id " + txt[j].getId());
        allTextView.add(txt[j]); /* add textview into arraylist */
        linelay_wordIn1.addView(txt[j], j);
    }
    return allTextView;

    }
此处:数字字存储在字符串数组中,在TextView中设置为文本

 /* sentence here */
static String[] words1 = { "I ", "like ", "to ", "ski." };
static String[] words2 = { "When ", "the ", "snow ", "falls, ", "I ",
    "head ", "to ", "the ", "slopes." };
static String[] words3 = { "My ", "Children ", "join ", "me ", "on ",
    "the ", "trip ", "to ", "the ", "hills. " };
static String[] words4 = { "We ", "ride ", "in ", "cable ", "cars ", "to ",
    "the ", "top ", "of ", "the ", "hill. " };
static String[] words5 = { "The ", "snow ", "sparkles ", "in ", "the ",
    "sunshine ", "as", "we ", "ski ", "the ", "trails. " };
static String[] words6 = { "I ", "pass ", "many ", "novice ", "skiers ",
    "as ", "they ", "struggle ", "to ", "stay ", "up. " };
static String[] words7 = { "I ", "try ", "to ", "watch ", "out ", "as ",
    "I ", "whiz ", "by. " };
static String[] words8 = { "If ", "I ", "do ", "not, ", "I ", "will ",
    "fall  ", "too. " };
下面是:一种方法可以在每次单击时显示textview数组x和y位置上的另一个toast

static void tooltip(String[] arr, int toolstringindex, int placeindex,
    float x, float y) {
FrameLayout layout = new FrameLayout(contextG);
layout.setBackgroundResource(R.drawable.tool_tip_img);

TextView tv = new TextView(contextG);
// set the TextView properties like color, size etc
tv.setTextColor(Color.BLACK);
tv.setTextSize(40);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
    android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
    android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
    Gravity.TOP);
lp.leftMargin = 10;
tv.setLayoutParams(lp);

// set the text you want to show in Toast
tv.setText(arr[toolstringindex]);
layout.addView(tv);
// // int[] values = new int[2];
float viewx = allTextView.get(placeindex).getX();
float viewy = allTextView.get(placeindex).getY();
// // int x = values[0];
// // int y = values[1];
System.out.println(" hello  x::" + x + 50 + "y :: " + y);
Toast toast = new Toast(contextG);
toast.setGravity(Gravity.CENTER_VERTICAL, (int) x, (int) y - 80);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

}
现在实际的问题是x和y的位置并不是完美的,它是 在屏幕上显示其他地方,所以,我不知道该怎么办?如果有的话 如果你对此有更好的想法,请让我来实施。一些 这行代码将对我有帮助。请帮助我。提前感谢

更改此项:

LinearLayout.LayoutParams lp;
    lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER;
为此:

LinearLayout.LayoutParams lp;
    lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.NO_GRAVITY;
lp.setMargins(x, y, maxX, maxY);

maxX和maxY不相关,只需将它们设置为文本宽度和高度。

不要使用n个文本视图和n个侦听器,使用一个侦听器在此处查找单词See TexrView.getLayout()方法-Layout类为您提供了在给定位置查找单词的简单api
LinearLayout.LayoutParams lp;
    lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.NO_GRAVITY;
lp.setMargins(x, y, maxX, maxY);