java选项卡>具有不同布局和操作的片段
我不熟悉安卓 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.
我从安卓 studio中给出的tabs-template
开始,但它要求每个tablayout
都是相同的。我已经看到了很多关于如何为每个选项卡设置不同布局的答案,但是如何对不同的选项卡进行编码(例如setonclicklistener
)
第二个问题是,每个解决方案都使用安卓,而我有安卓x
,所以导入不需要。在依赖关系中,我找不到设计V7
或类似的东西
主要活动。java:
package com.example.soloapp;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.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);
}
});
}
}
激活主控灯。xml:
<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
安卓:id="@+id/editNum"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginTop="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginBottom="8dp"
安卓:ems="10"
安卓:inputType="numberDecimal"
app:layout_constraintBottom_toTopOf="@+id/resultTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
安卓:id="@+id/resultTextView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginEnd="8dp"
安卓:layout_marginBottom="168dp"
安卓:text="Result"
app:layout_constraintBottom_toTopOf="@+id/calcButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
安卓:id="@+id/calcButton"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="8dp"
安卓:layout_marginEnd="8dp"
安卓:layout_marginBottom="200dp"
安卓:text="CALCULATE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</安卓x.constraintlayout.widget.ConstraintLayout>
# 1 楼答案
我将添加您需要学习的内容,以便创建您所说的内容
您需要在Android中使用
ViewPager
来创建可切换的选项卡。(如果你知道WhatsApp,那三个可切换的标签是ViewPager
)您可以了解有关
ViewPager
here的更多信息要使用
ViewPagers
,您需要了解什么是Fragments
,Android中的Fragments
类似于小活动(但不是活动)。您将这些Fragments
嵌入到Activities
中,因此需要将这些Fragments
放入包含ViewPager
的Activity
中您可以了解片段here
尽管如此,第一个
ViewPager
链接将足以让您了解创建可切换选项卡所需的一切关于你提到的第二个问题
根据Migrating to AndroidX
基本上,他们只是重命名了包名,这样他们就可以很容易地支持库的更新
您可以从这里轻松找到相应的androidx包
Migrating to Androidx.