简体   繁体   中英

Android/Java - setText to button within onbuttonclick cannot resolve method

First let me preface this with being completely new to Android development. I am trying to simply have text on a different buttons change when I click on "this" button. I can change the visibility fine with setVisibility() so I think I am referencing the buttons correctly - but when I attempt to setText() I get the error: "Cannot resolve method 'setText(Java.Lang.String)'"

Why will it allow me to change visibility but not text? What do I need to do to correct the problem?

Here's a portion of the XML for one of the buttons I am attempting to change text on:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/songbutton4"
    android:layout_below="@+id/button4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/button4"
    android:layout_alignEnd="@+id/button4"
    android:visibility="invisible" />

Here is the code for the onclick event:

public void onbutton1click(View v){
 //on click turn the 4 buttons invisible, and show the other 5
    View b1 = findViewById(R.id.button1);
    b1.setVisibility(View.INVISIBLE);
    View b2 = findViewById(R.id.button2);
    b2.setVisibility(View.INVISIBLE);
    View b3 = findViewById(R.id.button3);
    b3.setVisibility(View.INVISIBLE);
    View b4 = findViewById(R.id.button4);
    b4.setVisibility(View.INVISIBLE);
    View b5 = findViewById(R.id.button5);
    b5.setVisibility(View.INVISIBLE);

    View sb1 = findViewById(R.id.songbutton1);
    sb1.setVisibility(View.VISIBLE);
    sb1.setText("hello");  // THIS IS WHERE THE ERROR OCCURS
    View sb2 = findViewById(R.id.songbutton2);
    sb2.setVisibility(View.VISIBLE);
    View sb3 = findViewById(R.id.songbutton3);
    sb3.setVisibility(View.VISIBLE);
    View sb4 = findViewById(R.id.songbutton4);
    sb4.setVisibility(View.VISIBLE);
}

Are you doing it in Fragment or in Activity?

Anyway, you should do:

Button sb1 = (Button) findViewById(R.id.songbutton1);

This is how we add elements. If you set Button widget in your layout, in this way you need to have reference to this widget. Declaring proper type and casting using ( ).

If you are doing it in Fragment, not in Activity, you also need to bind it to proper view.

Button sb1 = (Button) v.findViewById(R.id.songbutton1);

And advice - don't get reference to views (widgets) in listener. Do it before listener.

Firstly assign the button to a variable ie: Button btnSong = (Button) findViewById(R.id.songbutton4);
then set an onclick listener to the button through the method btSong.setOnClickListener(); which will require you to declare a new onClickListener inside the button and a method will be created within which u can place the above code under the method. Alternatively add an XML attribute that points the button straight to the method.

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