gpt4 book ai didi

android - 我们如何在 Android 中创建类似 iPhone 的微调器?

转载 作者:可可西里 更新时间:2023-11-01 18:57:36 25 4
gpt4 key购买 nike

iPhone 上的默认微调器看起来比 Android 上的好得多。看起来至少有一个 Android 应用程序 (UrbanSpoon) 已经能够复制此控件,而且它很棒:http://www.urbanspoon.com/android

有人对如何创建它有任何想法吗?代码会有所帮助。

最佳答案

如果你不需要轨迹球的支持,那么只需要 WebView以及对一些现有的一些小调整 JavaScript that impersonates Apple's UIPickerView创建这样的应用程序。

Fancy Spinner Image

大部分艰苦的工作都是由 Matteo Spinelli 完成的,因此请从 downloading his code 开始然后将这些更改应用于 spinningwheel.js。他的代码想要从屏幕底部弹出带有取消和完成按钮的选择器,因此我们需要修改几行以消除此行为。

--- spinningwheel.js.orig   2010-05-26 00:17:00.411954051 -0700
+++ spinningwheel.js 2010-05-26 00:16:32.319010720 -0700
@@ -67,12 +67,10 @@

onOrientationChange: function (e) {
window.scrollTo(0, 0);
- this.swWrapper.style.top = window.innerHeight + window.pageYOffset + 'px';
this.calculateSlotsWidth();
},

onScroll: function (e) {
- this.swWrapper.style.top = window.innerHeight + window.pageYOffset + 'px';
},

lockScreen: function (e) {
@@ -113,9 +111,9 @@
// Create the Spinning Wheel main wrapper
div = document.createElement('div');
div.id = 'sw-wrapper';
- div.style.top = window.innerHeight + window.pageYOffset + 'px'; // Place the SW down the actual viewing screen
+ div.style.top = 0;
div.style.webkitTransitionProperty = '-webkit-transform';
- div.innerHTML = '<div id="sw-header"><div id="sw-cancel">Cancel</' + 'div><div id="sw-done">Done</' + 'div></' + 'div><div id="sw-slots-wrapper"><div id="sw-slots"></' + 'div></' + 'div><div id="sw-frame"></' + 'div>';
+ div.innerHTML = '<div id="sw-slots-wrapper"><div id="sw-slots"></' + 'div></' + 'div><div id="sw-frame"></' + 'div>';

document.body.appendChild(div);

@@ -164,8 +162,6 @@
window.addEventListener('scroll', this, true); // Reposition SW on page scroll

// Cancel/Done buttons events
- document.getElementById('sw-cancel').addEventListener('touchstart', this, false);
- document.getElementById('sw-done').addEventListener('touchstart', this, false);

// Add scrolling to the slots
this.swFrame.addEventListener('touchstart', this, false);
@@ -174,9 +170,6 @@
open: function () {
this.create();

- this.swWrapper.style.webkitTransitionTimingFunction = 'ease-out';
- this.swWrapper.style.webkitTransitionDuration = '400ms';
- this.swWrapper.style.webkitTransform = 'translate3d(0, -260px, 0)';
},


@@ -191,8 +184,6 @@

this.swFrame.removeEventListener('touchstart', this, false);

- document.getElementById('sw-cancel').removeEventListener('touchstart', this, false);
- document.getElementById('sw-done').removeEventListener('touchstart', this, false);

document.removeEventListener('touchstart', this, false);
document.removeEventListener('touchmove', this, false);

另外,他提供的index.html不是你想要的,换成这个,然后把html、css、js、png文件复制到你的assets目录下项目。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="spinningwheel.css" type="text/css" media="all" />
<script type="text/javascript" src="spinningwheel.js?v=1.4"></script>
<title></title>
<script type="text/javascript">

function getData() {
var results = SpinningWheel.getSelectedValues();
window.android.sendResults(results.values.join(' ') );
}

function notifyAndroid() {
window.android.readyForJavascript('');
}

</script>
</head>
<body onload="javascript:notifyAndroid()"></body>
</html>

创建一个在 WebView 中启用 JavaScript 并为其返回选择的回调。

public class FancySpinner extends Activity {
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.GetSelectedTimeButton))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
webView.loadUrl("javascript:getData()");
}
});
webView = (WebView) findViewById(R.id.WebView01);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new AndroidBridge(), "android");
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("file:///android_asset/index.html");
}
private class AndroidBridge {
public void sendResults(final String arg) {
Toast.makeText(FancySpinner.this, arg, Toast.LENGTH_SHORT).show();
}
public void readyForJavascript(final String arg) {
webView.loadUrl("javascript:SpinningWheel.addSlot({ " +
"1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6," +
"7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12 });");
webView.loadUrl("javascript:SpinningWheel.addSlot({ " +
"1: 'AM', 2: 'PM'});");
webView.loadUrl("javascript:SpinningWheel.open();");
}
}
}

最后,修改您的布局,使 WebView 的高度设置得当。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<WebView
android:id="@+id/WebView01"
android:background="#77CC0000"
android:layout_height="215dp"
android:layout_width="fill_parent"
android:focusable="false" />
<Button
android:text="Read Selected Time"
android:id="@+id/GetSelectedTimeButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

关于android - 我们如何在 Android 中创建类似 iPhone 的微调器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2909249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com