gpt4 book ai didi

java - Android 重用具有不同数据的 Activity

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:53 24 4
gpt4 key购买 nike

您好,我正在开发一个 Android 应用程序,并且有两个实际上相同但加载不同数据的 Activity 。我目前有两个包含大量重复代码的 Activity,我觉得我可以通过仅使用一个 Activity 来优化它。

Activity 1:

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.right_hearing_test);

String topHtml = this.getString(R.string.top_content);
String bottomHtml = this.getString(R.string.bottom_content);

View infoButton = findViewById(R.id.info_button);
infoButton.setVisibility(View.VISIBLE);

TextView titleText = (TextView) findViewById(R.id.title_text);
titleText.setText(R.string.Hearing_Test);

mScrollButton = (ScrollView) findViewById(R.id.scroll_view);

topContent = (WebView) findViewById(R.id.top_content);
topContent.setBackgroundColor(0);

bottomContent = (WebView) findViewById(R.id.bottom_content);
bottomContent.setBackgroundColor(0);

activityHelper = new ActivityHelper(this);

topContent.loadUrl("file:///android_asset/html/" + topHtml);
bottomContent.loadUrl("file:///android_asset/html/" + bottomHtml);

getScreenSize();
getMargins();

setResult(RESULT_OK);
}

Activity 2

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.left_hearing_test);

View infoButton = findViewById(R.id.info_button);
infoButton.setVisibility(View.VISIBLE);

mScrollButton = (ScrollView) findViewById(R.id.scroll_view);

topContent = (WebView) findViewById(R.id.top_content);
topContent.setBackgroundColor(0);

bottomContent = (WebView) findViewById(R.id.bottom_content);
bottomContent.setBackgroundColor(0);

String topHtml = this.getString(R.string.switch_file);
String bottomHtml = this.getString(R.string.bottom_content);

activityHelper = new ActivityHelper(this);

topContent.loadUrl("file:///android_asset/html/" + topHtml);
bottomContent.loadUrl("file:///android_asset/html/" + bottomHtml);

getScreenSize();
getMargins();
}

我将某些数据加载到 Activity 1 中的 WebView 和按钮中,然后用户进行测试,然后将用户带到 Activity 2。这里它所做的只是在 WebView 和按钮中显示不同的数据。

我的问题是,如果我为两个页面重复使用一个 Activity ,我如何将正确的数据加载到每个页面中,甚至可能吗?

通过传入上下文,我已经为我在这两个 Activity 中使用的许多其他方法使用了一个帮助程序类,但我只想为我在 web View 和按钮中显示的不同内容使用一个 Activity !

感谢任何输入!

最佳答案

只需保留一个标志来决定选择哪个选项..下面将告诉您如何控制它。

你可以控制这个标志 unisg getStringExtra(), putStringExtra()例如。您将从 FromActivity 类开始您的 Activity 。

FromActivity.java

.......    
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optionone");
startActivity(i);
.......

..
Intent i = new Intent(FromActivity.this,YourActivity.class);
i.putExtra("Flag","optiontwo");
startActivity(i);
...

你的 Activity .java

        @Override
public void onCreate(Bundle savedInstanceState) {

......
..
..
String flag = String.valueOf(getIntent().getStringExtra("Flag"));

if(flag.equalsIgnoreCase("optionone")){

String topHtml = this.getString(R.string.top_content);
String bottomHtml = this.getString(R.string.bottom_content);

TextView titleText = (TextView) findViewById(R.id.title_text);
titleText.setText(R.string.Hearing_Test);


}else if(flag.equalsIgnoreCase("optiontwo")){
String topHtml = this.getString(R.string.top_content);
String bottomHtml = this.getString(R.string.bottom_content);
}else{
}
.....
...
...
if(flag.equalsIgnoreCase("optionone")){
setResult(RESULT_OK);
}
....
}

关于java - Android 重用具有不同数据的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23091758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com