我继承了一个基本没有Java编程经验的Android Studio项目。我一直在使用免费的在线教程和The Big Nerd Ranch指南书教我自己,我知道如何使用相关的.java类切换到不同的.xml视图,但我想打开一个与之相关的新.xml视图相同的.java类。
基本上我只想修改我从蓝牙设备读取信息的应用程序,并在TextView中显示测量结果以及“通过/失败”显示,以显示通过/失败显示和“查看日志”按钮单击时,将转到包含带有所有结果的TextView的新.xml。
我试图避免使用不同的.java类,因为我希望所有项目具有相同的功能,只是在不同的.xml视图中,我担心将我已经拥有的所有内容都移动到新的中。 java类只会产生比我现有的问题更多的问题。
我不确定是否有必要,但这是我的代码: 从我的首屏开始
frament_bluetooth_scanner.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/go_to_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/view_log"
android:textSize="20dp"/>
<View
android:id="@+id/view"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#ccc"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:paddingTop="@dimen/margin_small"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<Button
android:id="@+id/button_present"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc4ab"
android:clickable="false"
android:padding="16dp"
android:text="NO TAGGANT DETECTED"
android:textSize="26dp"
/>
</LinearLayout>
button_present
都会激活蓝牙测试并显示通过/未通过结果。它存在并正在按预期工作。
go_to_log
是我添加的按钮,我希望它点击以转到下一个视图:
view_list.xml:
<FrameLayout
android:orientation="horizontal"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:visibility="visible"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="@string/clear_log"
android:id="@+id/button_clear_log" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="40dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/scanLogView"
android:maxLines = "4096"
android:text="@string/app_name"
android:scrollbars = "vertical"
android:layout_gravity="center_horizontal"/>
</FrameLayout>
所有这些代码最初都在fragment_bluetooth_scanner.xml
文件中,一切都按预期工作。唯一的问题是TextView
难以阅读,因为它显示了所有测试的信息,直到它被清除,并且即使是单个测试,所有信息都不适合屏幕。我想将其更改为更加用户友好。
主.java文件很长,所以我会发布它的相关部分。
来自ScannerFragment.java
:
@Override
public void onViewCreated(View view, @Nullable Bundle
savedInstanceState) {
SP = PreferenceManager.getDefaultSharedPreferences(getContext());
mPowerOffButton = (Button) view.findViewById(R.id.button_poweroff);
mDisconnectButton = (Button) view.findViewById(R.id.button_disconnect);
mConnectButton = (Button) view.findViewById(R.id.button_connect);
mPresence = (Button) view.findViewById(R.id.button_present);
mBattery = (ProgressBar) view.findViewById(R.id.progressBar);
mBlinkConnect = (Button) view.findViewById(R.id.button_connectionblink);
mBlinkData = (Button) view.findViewById(R.id.button_communication);
mClearLog = (Button) view.findViewById(R.id.button_clear_log);
mDeviceName = (Button) view.findViewById(R.id.button_devicename);
mDeviceSN = (Button) view.findViewById(R.id.button_devicesn);
mBatteryPerc = (TextView) view.findViewById(R.id.label_batterypct);
mReadingLog = (TextView) view.findViewById(R.id.scanLogView);
mReadingLog.setMovementMethod(new ScrollingMovementMethod());
mReadingLog.setText(readFromFile());
mViewLog = (Button)view.findViewById(R.id.go_to_log);
telephonyManager = (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
initLocationService(this.getContext());
final ActionBar actionBar = getActivity().getActionBar();
if (null == actionBar) {
return;
}
final Drawable D = getResources().getDrawable(R.drawable.stardust2);
actionBar.setBackgroundDrawable(D);
actionBar.setTitle("");
}
private void setupScanner() {
Log.d(TAG, "setupScanner()");
// Initialize the send button with a listener that for click events
mPowerOffButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(isIris) {
publishMessage(Constants.COMMAND_POWEROFF_IRIS);
} else {
publishMessage(Constants.COMMAND_POWEROFF);
}
mScannerService.stop();
}
});
// Initialize the send button with a listener that for click events
mDisconnectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Send a message using content of the edit text widget
publishMessage(Constants.COMMAND_DISCONNECT);
mScannerService.stop();
}
});
// Initialize the send button with a listener that for click events
mConnectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Send a message using content of the edit text widget
publishMessage(Constants.COMMAND_ON_CONNECT);
}
});
mClearLog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Send a message using content of the edit text widget
mReadingLog.setText("");
if(SP.getBoolean("writeToFile", true)) {
writeToFile("", "", false);
}
}
});
这是所有现有行动发生的地方。阻止我创建一个新的.java类的主要原因是整个类都使用mReadingLog
,我不明白此时读取/写入文本文件非常好,所以我想避免改变任何与该过程有关的事情。
我过去经历了this one之类的教程,但在与同一xml
相关联时,我找不到有关如何打开新.java
文件的任何内容类。这可能吗?
答案 0 :(得分:1)
......好的,所以这次我实际上读了......大声笑......你可以在一个班级中创建功能。将其设为静态,然后您可以在任何地方调用这些函数。
这就是我的工作
{
"name": "portfolio",
"version": "1.0.0",
"description": "Displaying self skills",
"main": "index.html",
"scripts": {
"start": "npm run lite",
"test": "echo \"Error: no test specified\" && exit 1",
"lite": "lite-server"
},
"author": "Vikranth Kanumuru",
"license": "ISC",
"devDependencies": {
"lite-server": "^2.3.0"
},
"dependencies": {
"bootstrap": "^4.0.0-alpha.6",
"font-awesome": "^4.7.0"
}
}
所以在我的任何课程中,我都可以这样打电话:
2017-08-18T20:23:16.489546+00:00 app[web.1]:
2017-08-18T20:23:16.489568+00:00 app[web.1]: > portfolio@1.0.0 start /app
2017-08-18T20:23:16.489569+00:00 app[web.1]: > npm run lite
2017-08-18T20:23:16.489570+00:00 app[web.1]:
2017-08-18T20:23:17.508804+00:00 app[web.1]:
2017-08-18T20:23:17.508810+00:00 app[web.1]: > portfolio@1.0.0 lite /app
2017-08-18T20:23:17.546486+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-08-18T20:23:17.508812+00:00 app[web.1]:
2017-08-18T20:23:17.518042+00:00 app[web.1]: sh: 1: lite-server: not found
2017-08-18T20:23:17.547397+00:00 app[web.1]: npm ERR! portfolio@1.0.0 lite: `lite-server`
2017-08-18T20:23:17.508811+00:00 app[web.1]: > lite-server
2017-08-18T20:23:17.552475+00:00 app[web.1]: npm ERR! Failed at the portfolio@1.0.0 lite script.
2017-08-18T20:23:17.547633+00:00 app[web.1]: npm ERR! spawn ENOENT
2017-08-18T20:23:17.547922+00:00 app[web.1]: npm ERR!
2017-08-18T20:23:17.555969+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-08-18T20:23:17.545582+00:00 app[web.1]: npm ERR! file sh
2017-08-18T20:23:17.546830+00:00 app[web.1]: npm ERR! errno ENOENT
2017-08-18T20:23:17.547103+00:00 app[web.1]: npm ERR! syscall spawn
2017-08-18T20:23:17.570546+00:00 app[web.1]:
2017-08-18T20:23:17.570802+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-08-18T20:23:17.623863+00:00 app[web.1]: npm ERR! portfolio@1.0.0 start: `npm run lite`
2017-08-18T20:23:17.570948+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2017-08-18T20_23_17_531Z-debug.log
2017-08-18T20:23:17.623455+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-08-18T20:23:17.623689+00:00 app[web.1]: npm ERR! errno 1
2017-08-18T20:23:17.624496+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-08-18T20:23:17.625545+00:00 app[web.1]:
2017-08-18T20:23:17.624015+00:00 app[web.1]: npm ERR! Exit status 1
2017-08-18T20:23:17.625895+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-08-18T20:23:17.624183+00:00 app[web.1]: npm ERR!
2017-08-18T20:23:17.626003+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2017-08-18T20_23_17_610Z-debug.log
2017-08-18T20:23:17.624342+00:00 app[web.1]: npm ERR! Failed at the portfolio@1.0.0 start script.
2017-08-18T20:23:17.788754+00:00 heroku[web.1]: Process exited with status 1
2017-08-18T20:23:17.804560+00:00 heroku[web.1]: State changed from starting to crashed
2017-08-18T20:24:22.252405+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=hidden-oasis-28106.herokuapp.com request_id=34b1dd02-e75a-400b-a7a6-7b611f60372f fwd="157.50.8.21" dyno= connect= service= status=503 bytes= protocol=https
2017-08-18T20:29:50.602309+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=hidden-oasis-28106.herokuapp.com request_id=3f4f9a84-3081-4b96-a078-89f404ddfeb3 fwd="157.50.8.21" dyno= connect= service= status=503 bytes= protocol=https
2017-08-18T20:35:04.704313+00:00 heroku[web.1]: State changed from crashed to starting
2017-08-18T20:35:06.664888+00:00 heroku[web.1]: Starting process with command `npm start`
2017-08-18T20:35:10.881400+00:00 app[web.1]:
2017-08-18T20:35:10.881410+00:00 app[web.1]: > npm run lite
2017-08-18T20:35:10.881410+00:00 app[web.1]: > portfolio@1.0.0 start /app
2017-08-18T20:35:10.881411+00:00 app[web.1]:
2017-08-18T20:35:11.913060+00:00 app[web.1]:
2017-08-18T20:35:11.913072+00:00 app[web.1]: > portfolio@1.0.0 lite /app
2017-08-18T20:35:11.913072+00:00 app[web.1]: > lite-server
2017-08-18T20:35:11.913073+00:00 app[web.1]:
2017-08-18T20:35:11.920446+00:00 app[web.1]: sh: 1: lite-server: not found
2017-08-18T20:35:11.944906+00:00 app[web.1]: npm ERR! file sh
2017-08-18T20:35:11.945211+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-08-18T20:35:11.945442+00:00 app[web.1]: npm ERR! errno ENOENT
2017-08-18T20:35:11.945920+00:00 app[web.1]: npm ERR! portfolio@1.0.0 lite: `lite-server`
2017-08-18T20:35:11.945674+00:00 app[web.1]: npm ERR! syscall spawn
2017-08-18T20:35:11.946095+00:00 app[web.1]: npm ERR! spawn ENOENT
2017-08-18T20:35:11.946309+00:00 app[web.1]: npm ERR!
2017-08-18T20:35:11.946516+00:00 app[web.1]: npm ERR! Failed at the portfolio@1.0.0 lite script.
2017-08-18T20:35:11.968813+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2017-08-18T20:35:11.946693+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-08-18T20:35:11.948500+00:00 app[web.1]:
2017-08-18T20:35:11.948745+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-08-18T20:35:11.948891+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2017-08-18T20_35_11_930Z-debug.log
2017-08-18T20:35:11.969079+00:00 app[web.1]: npm ERR! errno 1
2017-08-18T20:35:11.969447+00:00 app[web.1]: npm ERR! Exit status 1
2017-08-18T20:35:11.969275+00:00 app[web.1]: npm ERR! portfolio@1.0.0 start: `npm run lite`
2017-08-18T20:35:11.969622+00:00 app[web.1]: npm ERR!
2017-08-18T20:35:11.969809+00:00 app[web.1]: npm ERR! Failed at the portfolio@1.0.0 start script.
2017-08-18T20:35:11.970006+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2017-08-18T20:35:11.971505+00:00 app[web.1]:
2017-08-18T20:35:11.971728+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2017-08-18T20:35:11.971880+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2017-08-18T20_35_11_963Z-debug.log
2017-08-18T20:35:12.105152+00:00 heroku[web.1]: State changed from starting to crashed
2017-08-18T20:35:12.090364+00:00 heroku[web.1]: Process exited with status 1
2017-08-18T20:37:53.000000+00:00 app[api]: Build started by user saivicky2015@gmail.com
2017-08-18T20:38:19.813964+00:00 heroku[web.1]: State changed from crashed to starting
2017-08-18T20:38:19.435208+00:00 app[api]: Deploy bdaf4749 by user saivicky2015@gmail.com
2017-08-18T20:38:19.435208+00:00 app[api]: Release v7 created by user saivicky2015@gmail.com
2017-08-18T20:37:53.000000+00:00 app[api]: Build succeeded
2017-08-18T20:38:23.462016+00:00 heroku[web.1]: Starting process with command `npm start`
2017-08-18T20:38:27.290378+00:00 app[web.1]:
2017-08-18T20:38:27.290394+00:00 app[web.1]: > portfolio@1.0.0 start /app
2017-08-18T20:38:27.290395+00:00 app[web.1]: > npm run lite
2017-08-18T20:38:27.290395+00:00 app[web.1]:
2017-08-18T20:38:28.147286+00:00 app[web.1]:
2017-08-18T20:38:28.147298+00:00 app[web.1]: > portfolio@1.0.0 lite /app
2017-08-18T20:38:28.147299+00:00 app[web.1]: > lite-server
2017-08-18T20:38:28.147299+00:00 app[web.1]:
2017-08-18T20:38:29.567913+00:00 app[web.1]: Did not detect a `bs-config.json` or `bs-config.js` override file. Using lite-server defaults...
2017-08-18T20:38:29.570317+00:00 app[web.1]: ** browser-sync config **
2017-08-18T20:38:29.574243+00:00 app[web.1]: { injectChanges: false,
2017-08-18T20:38:29.574245+00:00 app[web.1]: files: [ './**/*.{html,htm,css,js}' ],
2017-08-18T20:38:29.574246+00:00 app[web.1]: watchOptions: { ignored: 'node_modules' },
2017-08-18T20:38:29.574247+00:00 app[web.1]: server: { baseDir: './', middleware: [ [Function], [Function] ] } }
2017-08-18T20:38:29.780891+00:00 app[web.1]: [Browsersync] Access URLs:
2017-08-18T20:38:29.781317+00:00 app[web.1]: ---------------------------------------
2017-08-18T20:38:29.781937+00:00 app[web.1]: Local: http://localhost:3000
2017-08-18T20:38:29.782215+00:00 app[web.1]: External: http://172.18.222.246:3000
2017-08-18T20:38:29.782426+00:00 app[web.1]: ---------------------------------------
2017-08-18T20:38:29.782691+00:00 app[web.1]: UI: http://localhost:3001
2017-08-18T20:38:29.782955+00:00 app[web.1]: UI External: http://172.18.222.246:3001
2017-08-18T20:38:29.783151+00:00 app[web.1]: ---------------------------------------
2017-08-18T20:38:29.783535+00:00 app[web.1]: [Browsersync] Serving files from: ./
2017-08-18T20:38:29.796322+00:00 app[web.1]: [Browsersync] Watching files...
2017-08-18T20:38:30.061505+00:00 app[web.1]: [Browsersync] Couldn't open browser (if you are using BrowserSync in a headless environment, you might want to set the open option to false)
2017-08-18T20:39:23.645673+00:00 heroku[web.1]: Process exited with status 137
2017-08-18T20:39:23.659964+00:00 heroku[web.1]: State changed from starting to crashed
答案 1 :(得分:1)
我不确定我是否理解。但是如果您想通过按下按钮或......来切换不同的视图,您可以使用ViewFilpper
在活动布局中将其添加为视图。
<ViewFlipper
android:id="@+id/simpleViewFlipper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Add View’s Here Which You Want to Flip -- >
<include layout="First"/>
<include layout="Second"/>
<include layout="third"/>
<!-- ... -->
</ ViewFlipper >