I am adding textviews dynamically to a linear layout and want to get the name of the textview clicked in OnClickListener of linear layout.This is the code:
m_lvSideIndex = (LinearLayout)ShowTheContacts1.this.findViewById(R.id.sideIndex);
TextView l_tempText = null;
for(int l_a = 0;l_a < m_arrayOfAlphabets.length;l_a++)
{
l_tempText = new TextView(ShowTheContacts1.this);
l_tempText.setGravity(Gravity.CENTER);
l_tempText.setTextSize(15);
l_tempText.setTextColor(getResources().getColor(R.color.black));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
l_tempText.setLayoutParams(params);;
l_tempText.setText(m_arrayOfAlphabets[l_a]);
m_lvSideIndex.addView(l_tempText);
m_lvSideIndex.setTag(l_a);
}
m_lvSideIndex.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String l_itemSelected = (String)v.toString(); //Want to get the name of textview selected here
});
Please help me.Thanks in advance.
将点击侦听器添加到每个文本视图,然后您将在onClick中将视图作为参数接收。
You can do with it the help of getTag()
first setTag() the value ie TextName
m_lvSideIndex.setTag(m_arrayOfAlphabets[l_a]);
m_lvSideIndex.setTag(l_a, R.id.sideIndex);
and get the value via getTag()
m_lvSideIndex.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String l_itemSelected = (String)v.getTag();
Integer l_position = (Integer)v.getTag(R.id.sideIndex);
});
OnClickListener works on TextView. Make sure you set Clickable property of TextView to true.
((TextView)v.findviewbyTag(R.id.label)).getText();
I hope this work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.