2013-12-19 167 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