簡體   English   中英

選項卡>具有不同布局+動作的片段

[英]Tabs > fragments with different layout + actions

我是android studio / java語言的新手。
我需要設置一個非常簡單的應用程序,但是我發現的信息無法讓我解決問題。
你們中的任何一個都可以幫忙嗎:)

我想制作一個帶有3個標簽的應用

  • (第一個選項卡)用戶輸入一個十進制數字,單擊按鈕后,結果將顯示一個通過公式計算得出的值

  • (第二個標簽)與第一個標簽相同,但具有值和公式

  • (第三個標簽)每個公式的信息

我已經為第一個標簽實現了(為此用途,簡化了)代碼。

我知道如何分別對所有三個選項卡進行編碼,但是我不知道如何在具有3個選項卡的一個應用程序中將它們合並在一起。

我從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. 您需要在Android中使用ViewPager創建可滑動標簽。 (如果您知道WhatsApp,則可滑動的三個選項卡是ViewPager )。

    您可以在此處了解有關ViewPager更多信息。

  2. 對於使用ViewPagers ,你需要了解哪些Fragments是, Fragments在Android的是像小活動(但不活動)。 您嵌入這些Fragments的內部Activities ,所以你需要把這些Fragments里面Activity包含您ViewPager

您可以在此處了解有關片段的信息

雖然,第一個ViewPager鏈接將足以讓您學習創建Swipeable Tabs所需的一切。

關於您提到的第二個問題

根據遷移到AndroidX

Androidx僅將原始支持庫API包映射到androidx命名空間。

基本上,他們只是重命名了程序包名稱,以便他們輕松支持庫的更新。

您可以從此處輕松找到相應的androidx軟件包。

遷移到Androidx。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM