Я пытаюсь сделать простое приложение с одним макетом и двумя кнопками, которые будут менять текст макета. Но я не могу найти решение, почему android:onClick не выполняется. И после попытки с OnClickListener, который не работал, но как здесь: from-setonclicklistene">Чем именно XML-атрибут android:onClick отличается от setOnClickListener?
Я прочитал и выполнил руководство на веб-сайте ANdroid SDK здесь: http://developer.android.com/reference/android/widget/Button.html
Это мой XML-код:
<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/maintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" />
Here is my Java code:
public class MainActivity extends Activity implements OnClickListener{
TextView label1;
int stage;
Button button1, button2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
label1 = new TextView(this);
TextView label1 = (TextView) findViewById(R.id.maintext);
stage = 1;
label1.setText("hello " + stage);
Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT"); //working
button1 = new Button(this);
button2 = new Button(this);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1_click(v);
Log.d("button1 working", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button2_click(v);
Log.d("button2 working", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
});
}
public void button1_click(View v) {
stage = 2;
label1.setText("hello " + stage);
Log.d("b1click", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
public void button2_click(View v) {
stage = 3;
label1.setText("hello " + stage);
Log.d("b2click", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}
@Override
public void onClick(View v) {
Log.d("wrong method", "THIS IS DEBUG OUTPUT TO LOGCAT"); // not appearing
}}
Таким образом, единственное сообщение Logcat, которое я получаю, - это сообщение в методе OnCreate. Переменная этапа всегда остается равной 1 и не изменяется, когда я нажимаю кнопки. Однако кнопки анимируют щелчок, поэтому это должен быть либо OnClick, либо сама метка, которая не работает.
Изображение работающего эмулятора: http://i.stack.imgur.com/ioUoD.png< /а>
Заранее спасибо!