2014-02-25 105 views
-2

如何獲取on-click()類中的按鈕上的文本設置? 我需要得到平方L處SELECT語句以編程方式設置並獲取android按鈕文本

tableLayout.addView(tableRow); 
    int a = 0; 
    for (Integer j = 0; j < count; j++) 
    { 
     Button b = new Button(getApplicationContext()); 
     b.setText(c.getString(c.getColumnIndex("jour"))); 
     b.setId(a++); 

     tableRow.addView(b); 
     b.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       Integer fff = v.getId(); 
       Toast.makeText(getApplicationContext(), fff.toString(), Toast.LENGTH_SHORT).show(); 


       Log.d("TAG", "The index is"); 
      } 
     }); 
     c.moveToNext() ; 


enter code here 

回答

1

您要添加的按鈕錶行tableRow.addView(b);按鈕上的文字。我首先看看沒有看到它的代碼。錯過了。

因此,使它最終

final Button b = new Button(getApplicationContext()); 
// Use ActivtiyContext 
final Button b = new Button(ActivityName.this); 
// posted a link at the end read it. 

匿名類不能在未聲明爲final或者有效最終其封閉範圍訪問本地變量。

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#accessing

裏面的onClick

public void onClick(View v) { 

     String value = b.getText().toString() 
     } 

還要檢查

When to call activity context OR application context?

+0

它就像現在的魅力:) – keshav

0
  • 首先你給像(buttonClick)按鈕onclick事件。

    final Button testButton = new Button(getApplicationContext()); 
    
    void buttonClick(View v){ 
    Log.v("text", testButton.getText().toString()); // get the text 
    testButton.setText("sometext"); //to change the text 
    } 
    
+0

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#accessing – Raghunandan

+0

@Raghunandan的感謝! – 2014-02-25 06:00:26

+0

'Final'不是'final' – Raghunandan

0

試試這個代碼:

public void onClick(View v) { 

    String value = (Button)v.getText().toString() 
    } 
1

按我的方式來創建String Array和:

String Title = new String[count]; 

而現在實行這樣的:

for (int j = 0; j < count; j++) 
{ 
    Button b = new Button(getApplicationContext()); 
    b.setText(c.getString(c.getColumnIndex("jour"))); 
    b.setId(j); 

    Title[a] = c.getString(c.getColumnIndex("jour"); 

    b.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v1) { 

      Toast.makeText(getApplicationContext(), "Button click on(): "+Title[v1.getId()].toString(), Toast.LENGTH_SHORT).show(); 

      Log.d("TAG", "The index is: "+v1.getId()); 
     } 
    }); 
    tableRow.addView(b); 
    c.moveToNext() ; 
    } 
1

您可以鍵入種姓按鈕的視圖並將其用於getText()。

tableLayout.addView(tableRow); 
int a = 0; 
for (Integer j = 0; j < count; j++) 
{ 
    Button b = new Button(getApplicationContext()); 
    b.setText(c.getString(c.getColumnIndex("jour"))); 
    b.setId(a++); 

    tableRow.addView(b); 
    b.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      Integer fff = v.getId(); 
      Toast.makeText(getApplicationContext(), fff.toString(),     
      Toast.LENGTH_SHORT).show(); 

      Button b = (Button)v; 
      String buttonText = b.getText().toString(); 
      Log.d("TAG", "The text is " + buttonText); 
     } 
    }); 
    c.moveToNext() ; 
相關問題