TextView onClick() not working

后端 未结 5 1964
一生所求
一生所求 2020-12-16 09:48

here\'s my code for main.xml

 
 

        
相关标签:
5条回答
  • 2020-12-16 10:06

    add android:clickable="true" in <TextView>

    0 讨论(0)
  • 2020-12-16 10:11

    There is an easy way. Put this in the TextView in XML:

    android:clickable="true"
    
    0 讨论(0)
  • 2020-12-16 10:20

    In your onCreate method you need to:

    1. Create variables for your TextViews
    2. Assign the onClickListener for them

    And your class also needs to implement OnClickListener.

    public class SqliteTestsActivity extends Activity implements OnClickListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            TextView all = (TextView) R.findViewById(R.id.viewall);
            all.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            // Fill in this with your switch statement
        }
    }
    
    0 讨论(0)
  • 2020-12-16 10:21

    Use these

     all = (TextView) this.findViewById(R.id.viewall);
     pdf = (TextView) this.findViewById(R.id.pic);
    

    in on create and then set

    all.setOnclickListener(this) in oncreate() method too.Implement onClicklistener when it will show error. it will work like a charm.

    Edited

    TextView btn=(TextView) findViewById(R.id.accInfo);
    btn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //DO you work here
            }
        });
    

    Setting Clicklistenner to TextView will automatically make it clickable so no need of

    android:clickable="true"
    
    0 讨论(0)
  • 2020-12-16 10:25

    please make changes like this :

     <include  android:id = "@+id/lyttab" layout="@layout/tabs" />
    

    in your Java class

     public void onClick(View v){   
    
      View view = findViewById(R.id.lyttab);
    
     all = (TextView) view.findViewById(R.id.viewall);
     pif = (TextView) view.findViewById(R.id.pic);
    
    
    switch (v){
    
    case  all :
        Toast.makeText(Main.this, "VIEW ALL", Toast.LENGTH_SHORT).show();
    
        all.setTextColor(getResources().getColorStateList(R.color.gradient_green));
        pic.setTextColor(getResources().getColorStateList(R.color.white));
    
        break;
    
    case pic:
        Toast.makeText(Main.this, "PDF", Toast.LENGTH_SHORT).show();
    
        all.setTextColor(getResources().getColorStateList(R.color.white));
        pic.setTextColor(getResources().getColorStateList(R.color.gradient_green));
        break;
    
     }
     }
    

    i think this will help you

    0 讨论(0)
提交回复
热议问题