java – Android重用不同数据的活动
发布时间:2020-12-15 04:19:32 所属栏目:Java 来源:网络整理
导读:嗨,我正在开发一个 Android应用程序,并有两个实际上相同的活动,但加载不同的数据.我目前有两个活动,有很多重复的代码,我觉得我可以通过只使用一个活动来优化它. 活动1: @Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedIn
嗨,我正在开发一个
Android应用程序,并有两个实际上相同的活动,但加载不同的数据.我目前有两个活动,有很多重复的代码,我觉得我可以通过只使用一个活动来优化它.
活动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); } 活动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(); } 我将某些数据加载到Web视图和活动1中的按钮,然后用户进行测试,然后将用户带到活动2.这里所做的只是在Web视图和按钮中显示不同的数据. 我的问题是,如果我为两个页面重用一个活动,如何将正确的数据加载到每个页面中,甚至可能? 我通过传递上下文,在两个活动中使用了很多其他方法的辅助类,但我想只对Webview和按钮中显示的不同内容使用一个活动! 感谢您的任何意见! 解决方法
只需保留一个标志即可决定选择哪个选项.
贝娄会告诉你如何控制它. 你可以控制这个标志unisg getStringExtra(),putStringExtra() FromActivity.java ....... Intent i = new Intent(FromActivity.this,YourActivity.class); i.putExtra("Flag","optionone"); startActivity(i); ....... 要么 .. Intent i = new Intent(FromActivity.this,"optiontwo"); startActivity(i); ... YourActivity.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); } .... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |