Qt Android和Native Date / TimePickerDialog看起来不同

时间:2016-12-26 00:05:46

标签: java android c++ qt

我在Android / Java中编写了Date / TimePickerDialog代码。然后,当我在Android 6.0上使用Android Studio运行此代码时,我看到:Android Native Image。然后当我在Qt / C ++中运行此代码时,我看到:Android Qt Image。 Java本机代码是相同的,但Android本机和Qt是不同的。测试设备也一样(Android 6.0 Phone)。为什么使用Android原生和Qt运行不同的相同代码?

Picker.java

import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import android.content.Context;

public class Picker extends org.qtproject.qt5.android.bindings.QtActivity
{
    private static Calendar calendar = Calendar.getInstance();
    private static String date, time;
    private static Context c;

    public Picker()
    {
    }

    public static String datePickerDialog(Context context)
    {
        c = context;
        DatePickerDialog datePickerDialog = new DatePickerDialog(c,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        date = i2 + "/" + i1 + "/" + i;
                        Toast.makeText(c, date, Toast.LENGTH_LONG).show();
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();

        return date;
    }

    public static String timePickerDialog(Context context)
    {
        c = context;
        TimePickerDialog timePickerDialog = new TimePickerDialog(c,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int i, int i1) {
                        time = i + " : " + i1;
                        Toast.makeText(c, time, Toast.LENGTH_LONG).show();
                    }
                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), false);
        timePickerDialog.show();

        return time;
    }
}

我的test.h文件

#ifndef TEST_H
#define TEST_H

#include <QObject>
#include <QtAndroid>
#include <QAndroidJniObject>

class Test : public QObject
{
  Q_OBJECT
public:
  explicit Test(QObject* parent = 0);
  Q_INVOKABLE QString datePickerDialog() const;
  Q_INVOKABLE QString timePickerDialog() const;

signals:

public slots:
};

#endif

我的test.cpp文件

#include "test.h"

Test::Test(QObject* parent) : QObject(parent)
{
}

QString Test::datePickerDialog() const
{
  QAndroidJniObject datePickerDialog;
  QtAndroid::runOnAndroidThread([&]()
  {
    datePickerDialog = QAndroidJniObject::callStaticObjectMethod(
        "com/xxx/qttest/Picker",
        "datePickerDialog",
        "(Landroid/content/Context;)Ljava/lang/String;",
        QtAndroid::androidActivity().object());
  });

  return datePickerDialog.toString();
}

QString Test::timePickerDialog() const
{
  QAndroidJniObject timePickerDialog;
  QtAndroid::runOnAndroidThread([&]()
  {
    timePickerDialog = QAndroidJniObject::callStaticObjectMethod(
        "com/xxx/qttest/Picker",
        "timePickerDialog",
        "(Landroid/content/Context;)Ljava/lang/String;",
        QtAndroid::androidActivity().object());
  });

  return timePickerDialog.toString();
}

0 个答案:

没有答案