Android Button OnClickListener Tutorial
In this tutorial you will learn how to use a button OnClickListener and make it clickable to open another activity by using intent. Here is how to do it:
- Open activity_main.xml file and then remove the hello world textview
and replace it with a button
- Give the button a label and we call it (Hello World) like this: android:text="Hello World"
- Now we need to position the button to the top-center of the screen like this:
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
- Next open up MainActivity.java and here is where we will declare the button as follows:
Button mButton = (Button) findViewById(R.id.button);
- To make the button clickable and perform an action we need to call setOnClickListener like this:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
- Now we have the button and it's clickable but it doesn't do any action, and what we really want is once the user who taps on the button will move to another page, so first we need to create an activity and resource file so that it will act as the second page like this:
click on File – New – Activity – Empty Activity then you give a name for the activity and the resource file.
- Once we have created the second activity, now we can work on the button setOnClickListener part to allow the user to move to another page.
This can be done like this:
startActivity(new Intent(MainActivity.this, SecondActivity.class));
MainActivity.this will be the current activity and starting point while the SecondActivity.class will like the destination
That's it :)
Source code: https://github.com/codingdemos/DemoButton
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Android Button OnClickListener Tutorial», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.