简体   繁体   中英

setOnClickListener in TextView

I'm trying to build an text view that goes to onClick but its not working they told me to add this code I did it but I'm having a lot of errors in it.

this is my MainActivity.java

     package imamalsajadsayings.android.com;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.TextView;

    public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
   public void runNextTask(){
       final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);
       final TrackerInfo newInfo = new TrackerInfo();
       //set up for model selection
       TextView modelTextview = (TextView)addView.findViewById(R.id.state1);                    
       modelTextview.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {

           }
       });
   }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

    }

and is my textview

        <TextView
        android:id="@+id/state1"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          android:text="@string/Tracker_model" 
        android:clickable="true"

note : I don't have addnewtracker created

errors :

 Description    Resource    Path    Location    Type
TrackerInfo cannot be resolved to a type    MainActivity.java   /ImamAlsajadsayings/src/imamalsajadsayings/android/com  line 20 Java Problem
addnewtracker cannot be resolved or is not a field  MainActivity.java   /ImamAlsajadsayings/src/imamalsajadsayings/android/com  line 19 Java Problem
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})  MainActivity.java   /ImamAlsajadsayings/src/imamalsajadsayings/android/com  line 23 Java Problem
TrackerInfo cannot be resolved to a type    MainActivity.java   /ImamAlsajadsayings/src/imamalsajadsayings/android/com  line 20 Java Problem
The method onClick(View) of type new OnClickListener(){} must override or implement a supertype method  MainActivity.java   /ImamAlsajadsayings/src/imamalsajadsayings/android/com  line 25 Java Problem
OnClickListener cannot be resolved to a type    MainActivity.java   /ImamAlsajadsayings/src/imamalsajadsayings/android/com  line 23 Java Problem

add to activity:

public void modelTextViewClick(View v) {
   //do something on click
}

and to layout:

<TextView android:id="@+id/state1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Tracker_model" 
          android:clickable="true"
          android:onClick="modelTextViewClick"/>

and remove your on click listener.

Change below code to your XML file

<TextView android:id="@+id/state1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/Tracker_model" 
      android:clickable="true"
      android:onClick="runNextTask"/>

and just modify your java file as follow.

public void runNextTask(View v){
   final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);
   final TrackerInfo newInfo = new TrackerInfo();
   //set up for model selection
   TextView modelTextview = (TextView)addView.findViewById(R.id.state1);                    
   modelTextview.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {

       }
   });

you need to import this package to avoid this error

missing this -> import android.view.View.OnClickListener;

  • You haven't call runNextTask from ur onCreate()
  • ur inflating a layout but didn't add that layout in any layout of ur activity_main layout using addView method

thats why it didn't work

it should be

package imamalsajadsayings.android.com;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.TextView;
    import android.view.View.OnClickListener;

    public class MainActivity extends Activity {
private LayoutInflater inflater;
private LinearLayout someLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    someLayout = (LinearLayout) findViewById(R.id.some_layout); //layout present in activity_main
    inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    runNextTask();

}
   public void runNextTask(){
       LinearLayout mInflatedLayout = (LinearLayout) inflater.inflate(R.layout.addnewtracker, null);
       final TrackerInfo newInfo = new TrackerInfo();
       //set up for model selection
       TextView modelTextview = (TextView)mInflatedLayout.findViewById(R.id.state1); 
       someLayout.addView(mInflatedLayout);                   
       modelTextview.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {

           }
       });
   }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

    }

Assuming ur using LinearLayout in ur activity_main layout

Use below piece of code.

package imamalsajadsayings.android.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
  public void runNextTask(){
   final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);
   final TrackerInfo newInfo = new TrackerInfo();
   //set up for model selection
   TextView modelTextview = (TextView)addView.findViewById(R.id.state1);                    

 }


@Override
 public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void modelTextViewClick(View view)
 {
   // here view reference is your text view reference. 
   // put your on click handler code.
 }
}

difference between this code and your code is onclick handler. In xml you have already defined a on click handler which you have to use in activity code. Other way set onclick listener handler foe your widget. Your doing both but either one of them is allowed not both for any widget in android.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM