Ничто внутри моего метода OnClick не работает, насколько я могу судить, и я понятия не имею, почему. Я использую эмулятор для проверки кода. Страница загружается правильно и есть две кнопки. Как видно из кода, я должен войти в журнал регистрации, в котором будут указаны две разные вещи, в зависимости от того, какая кнопка была нажата. Я также попытался добавить android: clickable = "true" в макете. Heres код:Почему моя работа OnClickListener не работает?
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>
Stack следы пожалуйста? – Actiwitty
определить: не работает .... это должно работать ... (просто небольшое замечание: лучше использовать 'v.getId() == R.id.select_male_avatar_btn' вместо' v == selectMaleAvtr') – Selvin
Что делать, если код вы выполняются не это ... попробуйте очистить проект .. и снова проверьте ... – challenger