javascript - Activity 按钮

标签 javascript android

我是 Android Studio 的新手,但正在尽力学习它。 这就是我想到的情况,不幸的是无法找到答案,尽管它看起来很基础。

我的应用程序以 3 buttons (Workouts, Results, Info) 开头

F.e 当我单击“WORKOUTS”时,我想进入我称为 WorkoutsActivity.java 的 Activity,其中有 12 buttons .

当我单击“RESULTS”时,我想访问 ActivityResults.java 等 Activity ...

  • 将这 3 个不同的按钮链接到 3 个不同的 Activity 的代码是什么?
  • 我需要在 WorkoutsActivity.java 中为按钮创建 12 个新 Activity 吗? (Android 就是这样工作的吗?)

非常感谢您的帮助。谢谢。

最佳答案

好的,在我发布源代码之前,您需要了解以下几个概念:布局 View Activity > Intent 事件

View 是可见的 UI 元素,例如文本、图像、按钮、进度条、评级栏等。

布局是不可见的 UI 元素,它们以定义的顺序显示 View ,例如行、列或相对于其他 View 的位置(toRightOf、toLeftOf 等)。它们也称为容器。

Activity 就像处理单个任务的“页面”。它包含 View 和布局(甚至更多)以及上下文。

Intent 是从另一个 Activity 到另一个 Activity 的桥梁。关于它们有很多东西需要学习,但基本上就像一座桥梁一样思考。

最后,事件是用户与 View 交互的方式:点击、触摸、拖放……它们包含监听器和处理程序,例如 OnClickListenerOnClick 。显然,第一个监听事件,第二个处理事件。

<小时/>
package com.learnandroid.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button myButton;

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

        myButton = (Button) findViewById(R.id.myButton);

        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(i);

            }
        });
    }
}

MainActivity.java

<?xml version="1.0" encoding="utf-8"?>

<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: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="com.learnandroid.myapplication.MainActivity">

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Button" />

</RelativeLayout>

activity_main.xml

不要忘记在 list 中声明每项 Activity :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.learnandroid.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>

</manifest>

AndroidManifest.xml

关于javascript - Activity 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37763900/

相关文章:

javascript - 如何为两列 <td> 添加单一渐变颜色?

javascript - 在 html5 canvas 标签中创建图像的分层树结构?

javascript - 为什么我的 JavaScript 警报不显示?

java - 为什么我不能从 android(Java) 中的另一个类更新类的变量?

java - php 页面中的 JSON 值未显示

android - Android音频效果限制为5个频段

android - 清除Android中所有数据库SQL表

javascript - 在 three.js 中调整 dat.gui 的大小

javascript - 高阶 javascript 函数

java - 找不到源错误并且应用程序突然关闭