如何在多个 Java 文件中使用相同的用户界面?ANDROID JAVA

huangapple go评论75阅读模式
英文:

How to use the same UI with multiple java files? ANDROID JAVA

问题

我是新手,正在开发一个单位转换器。

我想无论点击哪个按钮(重量或长度),都希望打开相同的 UI。
main_activity.xml UI

如何在多个 Java 文件中使用相同的用户界面?ANDROID JAVA

下面是我想要打开的 UI:activity_conversion.xml UI

如何在多个 Java 文件中使用相同的用户界面?ANDROID JAVA

我希望主活动中的每个按钮在不同的 Java 类上运行。
因此,除了 main_activity.java 和它的 .xml 文件之外,
我有 1 个 xml 文件(activity_conversion.xml)和 2 个 java 文件,分别用于 main_activity.xml 的每个按钮。

activity_main.xml 重量按钮

android:onClick="weightPage"

activity_main.xml 长度按钮

android:onClick="lengthPage"

MainActivity.java

public void weightPage(View view){
    Intent intent = new Intent(this, WeightActivity.class);
    startActivity(intent);
}

public void lengthPage(View view) {
    Intent intent2 = new Intent(this, LengthActivity.class);
    startActivity(intent2);
}

Length_Activity.java 长度按钮的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_conversion);
}

setContentView() 方法对我无效:(
提前感谢!

英文:

I'm a newbie and I'm working on a Unit Converter.

I would like to open the same UI regardless of which button I click(Weight or Length)
main_activity.xml UI

如何在多个 Java 文件中使用相同的用户界面?ANDROID JAVA

Below is the UI I want to open: activity_conversion.xml UI

如何在多个 Java 文件中使用相同的用户界面?ANDROID JAVA

And I would like each button in the main_activity to run on a different java class.
So, (minus the main_activity.java and it's .xml file)
I have 1 xml file(activity_conversion.xml) and 2 java files one for each button of the main_activity.xml

activity_main.xml Weight button

android:onClick="weightPage"

activity_main.xml Length button

android:onClick="lengthPage"

MainActivity.java

public void weightPage(View view){
    Intent intent = new Intent(this, WeightActivity.class);
    startActivity(intent);
}

public void lengthPage(View view) {
    Intent intent2 = new Intent(this, LengthActivity.class);
    startActivity(intent2);
}

Length_Activity.java code for Length button

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_conversion);
}

setContentView() method doesn't work for me:(
Thanks in advance!

答案1

得分: 1

> 我希望无论我点击哪个按钮(重量或长度),都能打开相同的UI界面。

您可以通过创建一个活动及其布局XML文件来实现此目的。然后通过显式意图启动该活动,如下所示:

//将此代码放入onClick方法内
Intent intent = new Intent(SoucreActivity.this, DestinationActivity.class);
startActivity(intent);

>而且我希望主活动中的每个按钮都在不同的Java类中运行。

不可以,屏幕上的所有UI元素始终在同一个活动中;它们不能在不同的Java类中运行。(除非您正在使用片段,作为新手您无需担心此事)

显然,您希望主活动中的两个按钮打开同一个活动。您可以使用上述代码片段中的意图来实现此目的。

英文:

> I would like to open the same UI regardless of which button I click(Weight or Length)

You can do that by creating an activity and its layout XML file. And then start that activity via explicit intent like this:

//Place this code inside the onClick method
Intent intent = new Intent(SoucreActivity.this, DestinationActivity.class);
startActivity(intent); 

>And I would like each button in the main_activity to run on a different java class.

No, you cannot. All UI elements on a screen are always in the same activity; they cannot run on different java classes. (Unless you are using fragments of which you need not worry about as you are a newbie)

Apparently, you want the two buttons in your main activity to open the same activity. Which you can achieve using intents using the code snippet mentioned above.

huangapple
  • 本文由 发表于 2020年10月16日 13:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定