Select a tableRow within a TableLayout Dynamically

后端 未结 4 1623
轻奢々
轻奢々 2021-01-15 23:57

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

for(int i = 0; i

        
相关标签:
4条回答
  • 2021-01-16 00:30

    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());
            }
        };  
    
    0 讨论(0)
  • 2021-01-16 00:33

    I found something cool and its working fine...

    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);
    
    0 讨论(0)
  • 2021-01-16 00:49

    Set on click listener on table row.

        tr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO:
            }
        });
    
    0 讨论(0)
  • 2021-01-16 00:52
     /************ 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;
              }        
    }
    
    0 讨论(0)
提交回复
热议问题