动态选择 TableLayout 中的 tableRow [英] Select a tableRow within a TableLayout Dynamically

查看:22
本文介绍了动态选择 TableLayout 中的 tableRow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建一个包含许多 TableRow 的 Tablelayout,例如:

I'm creating a Tablelayout with many TableRows dynamically, for example:

for(int i = 0; i<cont; i++)
                { 
                     id[i] = customers[i].CustomerNumber;

                     //Create a new row to be added.
                     tr = new TableRow(this);

                     //Create text views to be added to the row.
                     tv = new TextView(this);

                     //Put the data into the text view by passing it to a user defined function createView()
                     createView(tr, tv, id[i].ToString());


                     //Add the new row to our tableLayout tl
                     tl.AddView(tr);
                }    

这是创建视图代码:

private void createView(TableRow tr, TextView t, String viewdata) {

        t.SetText(viewdata, TextView.BufferType.Editable);

       //adjust the porperties of the textView

       //t.SetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

        //You have to use Android.Graphics.Color not System.ConsoleColor;
        t.SetTextColor(Color.Blue);
        t.SetBackgroundColor(Color.Cyan); 
        t.SetPadding(5, 0, 0, 0);

        tr.SetPadding(0, 1, 0, 1); 
        tr.SetBackgroundColor(Color.Black); 

        tr.AddView(t); // add TextView to row.

   }

我的问题是,我想从包含一行中的所有内容的 TableLayout 中进行选择,以便能够选择和响应单击事件,以便将其用于进一步目的.

My issue is that I want to select from the TableLayout that contains everything in a single row to be able to select and respond a click event in order to use it for further purpose.

推荐答案

更改您的代码以设置 TableRow Clickable tr.setClickable(true) 并添加一个 setOnClickListener:

change your code as for making TableRow Clickable set tr.setClickable(true) and add a setOnClickListener:

    private void createView(TableRow tr, TextView t, String viewdata) {

        t.SetText(viewdata, TextView.BufferType.Editable);

       //adjust the porperties of the textView

       //t.SetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

        //You have to use Android.Graphics.Color not System.ConsoleColor;
        t.SetTextColor(Color.Blue);
        t.SetBackgroundColor(Color.Cyan); 
        t.SetPadding(5, 0, 0, 0);

        tr.SetPadding(0, 1, 0, 1); 
        tr.SetBackgroundColor(Color.Black); 
        tr.setClickable(true);

        tr.setOnClickListener(tablerowOnClickListener);//add OnClickListener Here

        tr.AddView(t); // add TextView to row.


   }
private OnClickListener tablerowOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            //GET TEXT HERE
            String currenttext = ((TextView)v).getText().toString());
        }
    };  

这篇关于动态选择 TableLayout 中的 tableRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆