2012-02-04 76 views
0

enter image description here來自Android GridView字母的HighLight單詞?

我想在網格中定義字母,然後在選定的字符上突出顯示它,如果它是有效的字。 我可以定義字母並顯示在一個網格視圖中。但如何突出顯示單詞,如圖所示? 我正在開發類似的app.Please幫助。

我從Android Market上的Word Search App複製了圖像。

回答

1
  • 爲每個元素賦予唯一的ID - 如r1c1,r1c2等 - 行1 Col1,行1 Col2。這會在捕捉事件和設置顏色時幫助您。
  • 在網格的每個元素上捕獲onClicked/onSelected/onTouched。 你可以使用for循環爲每行& col。
  • 最初進行名義更改,如左邊的左邊的繪製邊界右對稱/直線 - 這使用戶可以識別所選項目。
  • 如果選定的元素形成一個合適的單詞,然後更改這些元素的背景顏色。

但在這裏,你會ahve要好好考慮下許多事情元素: 如果元素在直排或山坳選擇,您可以直接設置所有網格元素的背景。但如果它是對角選擇的,那麼你將不得不使用Drawable來增加顏色。您可以通過編程方式創建指定顏色的drawable並制定出來。

更新:

在XML中,只是給網格佈局。

在活動的onCreate(),就可以制定出這樣的添加內容:

grid = (GridLayout) findViewById(R.id.myGrid); 
TextView tv = null; 
ViewGroup tvLayParams = null; 

for (row=0; row < totalRows; row++) { 
    for (cols = 0; cols < totalCols; col++) { 
     // Create View 
     tv = new TextView(); 
     tv.setId("r" + row + "_c" + cols); // 1st row ids will be = r0_c0, r0_c1, r0_c2, r0_c3, etc 
     // Set properties like backgound color, etc of button 

     tvLayParams = (ViewGroup.LayoutParams) tv.getLayoutParams(); 
     // SET LayoutParams for Button 
     tv.setLayoutParams(btnLayParams); 

     tv.setOnClickListener(this); 
     tv.setOnTouchListener(this); 
     grid.addView(tv); 
    } 
} 


@Override 
public void onClick(View v) { 
    TextView tv = (TextView)v; 
    String id = tv.getId(); // From this you can get the Row & col of the item selected 
    int row = ; // For row, get digit(s) betweeen "r" and "_". 
    int col = Integer.parseInt(id.indexOf("c")+1); // For cols, get digit(s) from "c" 

    // Draw Borders/shade 
    //tv.setDrawable(...); // Generate drawable based on you need to draw dotted border or background and diagonally or straight. 

    // Figure out if the word is created 
    // If so, make another shade, else let it remain as currently selected & trying to make word 
    // Add your logic 
} 

這會給你對你的問題的啓動。開始並嘗試。 您也可以使用XML添加所有組件,而不是通過代碼添加它。但是這會讓你的XML變得非常漫長和混亂。

如果您卡住了,請隨時取用。爲了改進,你可以創建一個方法來創建按鈕視圖,並只需在inner for循環中調用teh方法。但是,一旦你成功地產生你想要的東西,就去做這件事。

但不要忘了先開始試用。

+0

任何示例代碼? – 2012-02-04 21:50:16

+0

http://stackoverflow.com/questions/5046320/a-grid-layout-of-icon-text-buttons - 類似問題的解決方案。以另一種方式查看更新後的答案。 – Tvd 2012-02-06 10:58:59

相關問題