java - 选项卡 > 具有不同布局 + 操作的 fragment

标签 java android tabs

我是 android studio/java 语言的新手。
我需要设置一个非常简单的应用程序,但我找到的信息并不能让我解决问题。
你们谁能帮忙:)?

我想制作一个带有 3 个选项卡的应用

  • (第一个选项卡)用户输入一个十进制数字,单击按钮后结果将显示通过公式计算得出的值

  • (第二个选项卡)与第一个选项卡相同,但包含值和公式

  • (第三个选项卡)每个公式的信息

我已经为第一个选项卡实现了(为此用途,简化的)代码。

I know how to code all the three tabs separately, but I don't know how to merge them together in one app with 3 tabs.

我从 android studio 中给出的 tabs-template 开始,但它要求每个 tablayout 都是相同的。我已经看到了很多关于如何为每个选项卡设置不同布局的答案,但是如何对不同的选项卡进行编码(例如 setonclicklistener)。

第二个问题是每个解决方案都使用 android,而我有 androidx,因此导入不会进行。在依赖项中,我没有找到设计 V7 或类似的内容。

<小时/>

Mainactivity.java:

package com.example.soloapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
    DecimalFormat numberFormat = new DecimalFormat("#.0");

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

        Button calcButton = (Button) findViewById(R.id.calcButton);
        calcButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                EditText editNum = (EditText) findViewById(R.id.editNum);
                TextView resultTextView = (TextView) findViewById(R.id.resultTextView);

                double cysC = Double.parseDouble(editNum.getText().toString());
                double tempResult = Math.pow(cysC,-0.931);
                double resultLong = 70.69 * tempResult;
                String result = numberFormat.format(resultLong);
                resultTextView.setText(result);

            }


        });

    }
}
<小时/>

activy_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintBottom_toTopOf="@+id/resultTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="168dp"
        android:text="Result"
        app:layout_constraintBottom_toTopOf="@+id/calcButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/calcButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="200dp"
        android:text="CALCULATE"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

我将添加您需要学习的内容,以便创建您所告诉的内容。

  1. 您需要使用ViewPager在 Android 中创建可滑动的选项卡。 (如果您了解 WhatsApp,那些可滑动的三个选项卡是 ViewPager)。

    您可以了解更多关于ViewPager的信息here .

  2. 使用ViewPagers ,你需要学习什么Fragments是,Fragments在Android中就像小 Activity (但不是 Activity )。您嵌入这些 Fragments在你的Activities里面,所以你需要把这些 Fragments里面Activity其中包含您的 ViewPager .

您可以了解 fragment here .

虽然,第一个ViewPager链接足以让您了解有关创建可滑动选项卡所需的所有知识。

关于您提到的第二个问题

根据Migrating to AndroidX ,

Androidx only maps the original support library API packages into the androidx namespace.

基本上,他们只是重命名了包名称,以便他们可以轻松支持库的更新。

您可以从这里轻松找到相应的androidx包。

Migrating to Androidx.

关于java - 选项卡 > 具有不同布局 + 操作的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56496703/

相关文章:

java - hornetq "listMessagesAsJson"不工作

android - EditText setOnFocusChangeListener 在所有 EditText 上

android - UnManaged 选项在 google play developer console 中不可用

java - Android 异常但没有可见错误

java - 如何在JAVA中找到区分大小写的字符串并忽略它

java - 禁用 springframework 的日志记录

java - 具有内部数组类的数组 pojo 类的 RecyclerView 适配器

jquery - 使用 Asp.Net 按钮选择选项卡

matlab - MATLAB GUI 中的优化选项卡

javascript - 为什么 setTimeout 在此函数中不起作用?