【问题标题】:Select a tableRow within a TableLayout Dynamically动态选择 TableLayout 中的 tableRow
【发布时间】:2012-07-06 02:46:33
【问题描述】:

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

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 代码:

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 中进行选择,以便能够选择并响应单击事件,以便将其用于进一步的目的。

【问题讨论】:

    标签: android xamarin.android tablelayout tablerow


    【解决方案1】:

    更改代码以使 TableRow Clickable 设置为 tr.setClickable(true) 并添加 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());
            }
        };  
    

    【讨论】:

    • 如何获取行的id?
    • 你是否设置了row id?
    • 如果您还有问题,请告诉我,我会尽力帮助您?
    【解决方案2】:

    在表格行上设置点击监听器。

        tr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO:
            }
        });
    

    【讨论】:

      【解决方案3】:

      我发现了一些很酷的东西,而且效果很好......

      TableLayout tl=(TableLayout)findViewById(R.id.maintable);
      
      ....
      
      TableRow tr1 = new TableRow(this);
      tr1.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
      
      
      
      TextView textview1 = new TextView(this);
      textview1.setText(etFirstname.getText());
      textview1.setPadding(5, 0, 0, 0);
      textview1.setTextColor(Color.YELLOW);
      textview1.setBackgroundColor(Color.GREEN);
      tr1.addView(textview1);
      
      TextView textview2 = new TextView(this);
      textview2.setText(etAge.getText());
      //textview2.setText(etAge.getText());
      textview2.setPadding(5, 0, 0, 0);
      textview2.setTextColor(Color.RED);
      textview2.setBackgroundColor(Color.GRAY);
      tr1.addView(textview2);
      
      
      
      tr1.setOnClickListener(new OnClickListener() {
      
          @Override
          public void onClick(View v) {
              TableRow tr1=(TableRow)v;
              TextView tv1= (TextView)tr1.getChildAt(0);
      
              Toast.makeText(getApplicationContext(),tv1.getText().toString(),Toast.LENGTH_SHORT).show();
      
          }
      });
      
      
      tl.addView(tr1);
      

      【讨论】:

        【解决方案4】:
         /************ if table row is dynamic then this method else method 2 is perfect************/
        //if we want to applay listener on dynamic tablerow then use this
        //sure that perfect 
         TablRowe tr = new TableRow(this);
        tr.setClickable(true);
        tr.setId(100);// if in loop then add 1 counter with 100 like (100+counter) at end count++ it
         tr.setOnClickListener(this);
         @Override
            public void onClick(View v) 
        {
                switch (v.getId())
                 {
                 case 100: 
                       Toast.makeText(getApplicationContext(), "100", Toast.LENGTH_SHORT).show();   
                     break;
        
                 case 101:
                    Toast.makeText(getApplicationContext(), "101", Toast.LENGTH_SHORT).show();  
                     break;
        }
        /************************** for simple like this ************************/
           TableRow row1 = (TableRow)findViewById(R.id.row1);
        row1.setonClickListener(this);
        public void onClick(View v)
        {
        switch (v.getId())
                 {
                 case R.id.row1: 
                   // do work
                     break;
                  }        
        }
        

        【讨论】:

        • 请花点时间记录您提供此代码作为答案的原因。为什么是正确的;有什么不同?
        • 我认为这是完美的
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        • 1970-01-01
        • 2018-09-19
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多