2012-06-23 322 views

回答

0

然而,無論您想要做什麼,都可以創建視圖並立即添加偵聽器。

TextView tv = new TextView(ActivityName.this); 
tv.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Toast.makeText(ActivityName.this, "tv text: " + ((TextView) v).getText().toString()).show(); 
     //Do whatever you want to do here. 
    } 
}); 
layout.addView(tv); //layout added on the xml for example, or by an inflater. 
2

你應該檢查這段代碼。創建一個onclicklistener,然後使用setOnClickListener();方法。

private TextView textview1, textview2; 
//initialize them  

OnClickListener customTextviewOnClicklistener = new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      if (v == textview1) 
      { 
       // Here your code for textview1 
       Log.i("Clicked Item", "textview1"); 
      } 
      else if (v == textview2) 
      { 
       // Here your code for textview2 
       Log.i("Clicked Item", "textview2"); 
      } 
      else 
      { 
       //Here your code for others 
      } 
     } 
    }; 

    textview1.setOnClickListener(customTextviewOnClicklistener); 
    textview2.setOnClickListener(customTextviewOnClicklistener); 

我希望這可以幫助你。

相關問題