如何使用工具栏从片段回到活动

时间:2018-12-28 10:03:56

标签: android kotlin

在MainActivity中,通过菜单打开FavoriteFragment,我想使用工具栏 setStyleSheet("background-color: hsv("+QString::number(H_hsv)+", " +QString::number(S_hsv * 100)+"%, "+QString::number(V_hsv * 100)+"%);");

返回

有趣的#include <QtWidgets/QApplication> #include <qwidget.h> #include <qmainwindow.h> #include <cmath> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow* main = new QMainWindow; QWidget* central = new QWidget(); double S_hsl = 1; double L_hsl = 1; int H_hsl = 120; int H_hsv = H_hsl; // This is the easy one of course double V_hsv = (2 * L_hsl + S_hsl * (1 - abs(2 * L_hsl - 1))) / 2; double S_hsv = (2 * (V_hsv - L_hsl)) / V_hsv; central->setStyleSheet("background-color: hsv(" + QString::number(H_hsv) + ", " + QString::number(S_hsv * 100) + "%, " + QString::number(V_hsv*100) + "%);"); main->setCentralWidget(central); main->resize(600, 400); main->show(); return a.exec(); }

setDisplayHomeAsUpEnabled()

有趣的onBackPressed

onOptionsItemSelected

在我的片段中,我创造了有趣的 R.id.favorite -> { fragmentManager = supportFragmentManager val mainFragment: FavoriteFragment = FavoriteFragment().newInstance() fragmentManager!!.beginTransaction().replace(R.id.frameLayout, mainFragment) .addToBackStack("FAVORITE_FRAGMENT").commit() true }

    override fun onBackPressed() {
    if (supportFragmentManager.findFragmentByTag("FAVORITE_FRAGMENT") != null) {
        supportFragmentManager.popBackStackImmediate("FAVORITE_FRAGMENT", 0);
    } else {
        super.onBackPressed();
    }

}

物理后退按钮有效,但工具栏后退无效

1 个答案:

答案 0 :(得分:0)

您可以尝试此代码段。 (对不起Java代码,我不知道kotlin,您必须手动将其转换)

Public Function intPosition(testStr As String) As Integer
    Dim x%
    For x = 1 To Len(testStr)
        If Mid(testStr, x, 1) Like "#" Then
            intPosition = x
            Exit For
        Else
            intPosition = 1
        End If
    Next x
End Function