2013-12-19 118 views
0

这是我按照句子中的单词实现textview数量的方法。每个单词都有点击事件或触摸事件。动态textviews没有完美的点击监听器或触摸监听器

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; 

    } 

我触摸监听器代码是在这里

public static OnTouchListener myListner = new OnTouchListener() { 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    // TODO Auto-generated method stub 
    TextView tv = (TextView) v; 

    //Layout layout = ((TextView) v).getLayout(); 
    String str = tv.getText().toString(); 
    int x = (int) event.getX(); 
    int y = (int) event.getY(); 
    //if (layout != null) { 
    //int line = layout.getLineForVertical(y); 
    //int characterOffset = layout.getOffsetForHorizontal(line, x); 
    //Log.i("index", "" + characterOffset); 
    //} 
    System.out.println(" str of sentence :: " + str.toString()); 

    if (Music.playing()) { 
    // do nothing 
    } else { 
    try { 
     /* Music renew for memory leak problems */ 
     Music.renewMediaPlayer(); 
     /*--------------------*/ 
     if (str.equalsIgnoreCase("I ")) { 
     Music.play(contextG, sent_audio1[0]); 
     } else if (str.equals("like ")) { 
     Music.play(contextG, sent_audio1[1]); 
     } else if (str.equals("to ")) { 
     Music.play(contextG, sent_audio1[2]); 
     } else if (str.equals("ski.")) { 
     Music.play(contextG, sent_audio1[3]); 
     tooltip(tool1, 0, 3, x, y);// tool1 is for guide text 
         // array 
         // and 0 
         // for index 
     } else if (str.equals("When ")) { 
     Music.play(contextG, sent_audio2[0]); 
     tooltip(tool2, 0, 0, x, y); 
     } else if (str.equals("the ")) { 
     Music.play(contextG, sent_audio2[1]); 
     tooltip(tool2, 0, 1, x, y); 
     } else if (str.equals("snow ")) { 
     Music.play(contextG, sent_audio2[2]); 
     tooltip(tool2, 0, 2, x, y); 
     } else if (str.equals("falls, ")) { 
     Music.play(contextG, sent_audio2[3]); 
     } else if (str.equals("head ")) { 
     Music.play(contextG, sent_audio2[5]); 
     tooltip(tool2, 1, 5, x, y); 
     } else if (str.equals("slopes.")) { 
     float x1 = event.getX(); 
     float y1 = event.getY(); 
     Music.play(contextG, sent_audio2[8]); 
     tooltip(tool2, 2, 8, x1, y1); 
     } 

    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
    } 
    return true; 

} 
}; 

这里:字数店字符串数组设置为textviews文本

/* 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位置上进行每次点击。

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位置不来完美。它的 显示别的地方在屏幕上的话,我没有得到它做什么?如果有任何 有一个更好的想法,请让我来实现这一点。一些 代码行将有助于me.please帮助我。在此先感谢

+0

dont creare n TextViews和n个监听器,使用一个监听器并在这里找到rhe word – pskink

+0

参见TexrView.getLayout()方法 - Layout类为您提供了简单的api,用于在给定位置查找单词 – pskink

回答

0

更改此:

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的美星和是不相关的只是设置为文本的宽度和高度。

+0

no它不给我输出 – Sydroid