我继承了一个基本没有Java编程经验的Android Studio项目。我一直在学习并使用免费的在线教程和 The Big Nerd Ranch 指南自学,我知道如何使用关联的 .java 类切换到不同的 .xml View ,但我想打开一个新的 .xml View 相同的 .java 类。
基本上我只是想修改我拥有的应用程序,该应用程序从蓝牙设备读取信息并在 TextView 中显示测量结果以及“通过/失败”显示以具有通过/失败显示和“查看日志”按钮单击时,会将您带到一个新的 .xml,其中包含带有所有结果的 TextView。
我试图避免使用不同的 .java 类,因为我希望所有项目都具有相同的功能,只是在不同的 .xml View 上,而且我担心将我已有的所有内容都移到新的 .java 中。 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 :
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 ,但我找不到任何关于如何在与相同的 .java
类关联时打开新的 xml
文件的信息。这可能吗?
最佳答案
我不确定我是否理解。但是如果你想通过按下按钮或...在不同的 View 之间切换,你可以使用 ViewFilpper .
在您的 Activity 布局中将其添加为 View 。
<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 >
关于java - 如何使用单个 java 类在多个 View 之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45764162/