Следуя руководству, я уже настраивал onClick-Listeners.
Теперь я пытаюсь сделать простое приложение для прослушивания одного потока, но мой onClickListener ничего не делает.
Я создал новое очень простое приложение в Android Studio 3.3.2, чтобы узнать, что происходит. Я думаю, это что-то очень простое.
package com.example.musicplay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
final Button play_button = findViewById(R.id.play_button);
Log.i("Button found","this one: " + play_button.getText());
play_button.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the play button is clicked on.
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "A click happened!", Toast.LENGTH_SHORT).show();
}
});
setContentView(R.layout.activity_main);
}
}
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical">
<Button
android:id = "@+id/play_button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/play_button"/>
</LinearLayout>
Тост-сообщение должно появиться при нажатии кнопки, но ничего не происходит.
спасибо, это было быстро...
Вы дважды вызываете setContentView. Это заменит иерархию представления, установленную первым вызовом. Удалите второй вызов setContentView.
почему вам нужно иметь два setContentView?