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

问题描述

我动态创建Tablelayout许多TableRows,例如:

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);
                }

这是CreateView的code:

And this is the createView code:

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.

   }

我的问题是,我想从一个包含一切都在一个单一的行可以选择,为了使用它的另一目的响应click事件的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.

推荐答案

更​​改code为作出的TableRow可点击设置 tr.setClickable(真)并添加一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:38