Why isnt my implementation of OnClickListener working?

时间:2015-09-01 21:38:35

标签: java android android-studio onclick onclicklistener

Nothing inside of my OnClick method is working as far as I can tell and I have no idea why. Im using an emulator to test out the code. The page loads properly and there are two buttons. As seen in the code, I'd log to do an info log saying two different things depending on which button was pressed. I also tried adding android:clickable="true" in the layout. Heres the code:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class UserOnboardActivity extends ActionBarActivity implements OnClickListener {

    private Button selectMaleAvtr;
    private Button selectFemaleAvtr;

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

        selectMaleAvtr = (Button)findViewById(R.id.select_male_avatar_btn);
        selectFemaleAvtr = (Button)findViewById(R.id.select_female_avatar_btn);

        selectMaleAvtr.setOnClickListener(this);
        selectFemaleAvtr.setOnClickListener(this);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        Log.i("whaaat", v.toString());
        Log.i("whaaat", "hello");
        Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG)
                .show();
        if(v==selectMaleAvtr) {
            Log.i("UserOnboardActivity", "male avatar button pressed");
        } else if (v==selectFemaleAvtr) {
            Log.i("UserOnboardActivity", "female avatar button pressed");
        }
    }
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.incubatorcle.dinahealth.UserOnboardActivity">

    <TextView android:text="@string/select_avatar_user_onboard" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/select_avatar_tv"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:soundEffectsEnabled="true"
        android:gravity="center_horizontal"
        android:textSize="20sp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/select_avatar_btns_lin_lay">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:id="@+id/select_female_avatar_btn"
            android:layout_alignTop="@+id/select_male_avatar_btn"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:id="@+id/select_male_avatar_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"/>
    </LinearLayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

I guess the issue is you have to look for the viewids instead of the view itself:

You can either do:

public void onClick(View v) {
        Log.i("whaaat", v.toString());
        Log.i("whaaat", "hello");
        Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG)
                .show();
        if(v.getId()==R.id.selectMaleAvtr) {
            Log.i("UserOnboardActivity", "male avatar button pressed");
        } else if (v.getId()==R.id.selectFemaleAvtr) {
            Log.i("UserOnboardActivity", "female avatar button pressed");
        }
    }

OR

public void onClick(View v) {
    switch (v.getId()) {

    case R.id.selectMaleAvtr:
        // Things you want do
        break;

    case R.id.selectFemaleAvtr:
        // Things you want to do
        break;

    default:
        break;
    }

}

答案 1 :(得分:0)

问题是我使用不同的活动来打开视图,因此此活动中的代码永远不会被加载。我通过使用intent然后使用startActivity来解决问题。