我有這段代碼來創建一個動態表。 現在我想附加到onClick的每個TextView事件。當我點擊一個TextView時,我想獲取該值並執行一個方法。 所以這是我的代碼:如何使用OnClickListener從TextView中獲取值?
TableLayout ll=(TableLayout) view.findViewById(R.id.displayLinear);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
TableRow.LayoutParams itemParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f);
int i = 1;
for (Iterator<OperazioniClienti> it = listaOperazioni.iterator(); it.hasNext();) {
OperazioniClienti op = it.next();
TableRow row= new TableRow(getActivity());
//TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
//row.setLayoutParams(lp);
row.setLayoutParams(rowParams);
data.setBackgroundResource(R.drawable.shape_edittext_row);
data.setLayoutParams(itemParams);
TextView codFattura = new TextView(getActivity());
codFattura.setText(op.getCodiceFattura().toString());
codFattura.setBackgroundResource(R.drawable.shape_edittext_row);
codFattura.setLayoutParams(itemParams);
Double saldo = op.getSaldo();
TextView importo = new TextView(getActivity());
importo.setLayoutParams(itemParams);
importo.setBackgroundResource(R.drawable.shape_edittext_row);
importo.setGravity(Gravity.RIGHT);
importo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//to do
}
});
importo.setText("SALDATA");
importo.setTextColor(Color.BLUE);
row.addView(codFattura);
row.addView(importo);
ll.addView(row,i++);
}
}
我使用此代碼的onclick事件附加到所有的TextView,但我不知道我怎樣才能得到的值:
importo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//to do
}
});