2016-01-04 53 views
-1

我試圖從視圖中返回一個按鈕對象,隨後運行setText。 雖然我很難轉換它。 任何意見讚賞。從視圖中獲取按鈕

public Button aiPlayerPick() { 
     Button btn = null; 
     TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout); 

     for (int rowIndex = 0; rowIndex < tableLayout.getChildCount(); rowIndex++) { 
      View tableLayoutChild = tableLayout.getChildAt(rowIndex); 
      if (tableLayoutChild instanceof TableRow) { 
       for (int i = 0; i < ((ViewGroup) tableLayoutChild).getChildCount(); i++) { 
        View view = ((ViewGroup) tableLayoutChild).getChildAt(i); 
        if (view instanceof Button && view.getTag() == aiPickedButton) { 

         View btn_v = view.findViewWithTag(aiPickedButton); 
         System.out.println("Button: " + btn_v); 
         btn = (Button) findViewById(R.id.btn_v); // Problem is here, I can't get that view to be seen as a button 

         break; 
        } else { 
         i++; 
        } 
       } 
      } 
     } 
     return btn; 
    } 
+0

試過,但它仍然有或多或少相同的問題。 'btn_v'不被識別 –

+0

btn =(Button)view.findViewWithTag(aiPickedButton);只是使用你已經擁有它 – Xjasz

回答

1

你已經獲取的View(該Button的父級)對你的代碼:

View btn_v = view.findViewWithTag(aiPickedButton); 

所以,你需要做的唯一事情是將它轉換成一個Buttonbtn = (Button) someView。你的代碼看起來像:

View btn_v = view.findViewWithTag(aiPickedButton); 
System.out.println("Button: " + btn_v); 
btn = (Button) btn_v; 
System.out.println("Button: " + btn);