简体   繁体   中英

ImageButton not doing anything on click - android

Im doing the front-end of my new app on android, and I came across with a problem. On my first Activity my button works fine and take the user to the second Activity, now the problem appear. When I click on another button to take me to a third Activity nothing happens.

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

    ImageButton saldoButton = (ImageButton)findViewById(R.id.saldoButton);

    saldoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.activity_saldo);

        }
    });
}

now follow my xml of this button:

            <ImageButton
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:id="@+id/saldoButton"
            android:layout_column="2"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/test02" />

Anyone has a clue what is happening?

You have to start third activity

saldoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             Intent i= new Intent(currentActivity.this,thirdActivity.class);
             startActivity(i);

        }
    });

You are missing the android:clickable="true" on your XML.

        <ImageButton
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/saldoButton"
        android:layout_column="2"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/test02" />

And then on your class activity

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

    ImageButton saldoButton = (ImageButton)findViewById(R.id.saldoButton);

    saldoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("This Class", "I am a working button!");
            Intent intent = new Intent(this, NewClass.class);
            this.startActivity(intent);

        }
    });
}

it should work now. If it doesn't, please tell me. Good luck!

Since you are inflating a new layout, you need to call once again findViewById to regain access to your ImageView since the ID of your button in one layout layout is not the same in another layout.

saldoButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        setContentView(R.layout.activity_saldo);
        saldoButton = (ImageView) findViewById(R.id.saldoButton);
    }
});

That being said, you should create another activity or another fragment instead of inflating another layout.

add android:clickable="true" in ImageButton XML.

Define and Initialization AudioManger.

var audioManager:AudioManager = context.getSystemService(Context.AUDIO_SERVICE) 
as AudioManager

after that.

imageButton.setOnClickListener(View.OnClickListener { view ->
        audioManager.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN,1f);
    })

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