你如何实现十进制键盘?

Posted

技术标签:

【中文标题】你如何实现十进制键盘?【英文标题】:How do you implement a decimal keyboard? 【发布时间】:2021-09-27 01:41:25 【问题描述】:

在我的视图结构中注释掉了,我想在填写长/纬度文本字段时限制用户使用的键盘。还有其他线程接近这个问题,但我不明白他们在答案中引用了什么。

如何限制用户使用小数点+标点键盘?

//
//  ContentView.swift
//  Geo Calculator App
//  App that calculates distance between two long/lat values
//
//  Created on 9/25/21.
//

import SwiftUI

struct ContentView: View 
    @State var longitude1 = ""
    @State var latitude1 = ""
    @State var longitude2 = ""
    @State var latitude2 = ""
    //self.LongLatTextField.keyboardType = UIKeyboardType.decimalPad
    var body: some View 
        VStack 

            Spacer()
            
            Text("Geo Calculator App")
                .font(.system(size: 24, weight: .bold))
                .foregroundColor(.black)
            
            Text("Calculate distances between long/lat values")
                .font(.system(size: 16, weight: .light))
                .foregroundColor(.gray)
            
            
            HStack 
            HStack 
                TextField("Longitude A", text: $longitude1)
                    .foregroundColor(.blue)
            .frame(height: 60)
                .padding(.horizontal, 15)
                .background(Color.white)
                .cornerRadius(9)
            
            HStack 
                TextField("Latitude A", text: $latitude1)
                    .foregroundColor(.blue)
            .frame(height: 60)
                .padding(.horizontal, 15)
                .background(Color.white)
                .cornerRadius(9)
            .frame(height: 60)
                .padding(.horizontal, 15)
                .cornerRadius(9)
                .padding(.horizontal, 5)
            
            HStack 
            HStack 
                TextField("Longitude B", text: $longitude2)
                    .foregroundColor(.blue)
            .frame(height: 60)
                .padding(.horizontal, 15)
                .background(Color.white)
                .cornerRadius(9)
            
            HStack 
                TextField("Latitude B", text: $latitude2)
                    .foregroundColor(.blue)
            .frame(height: 60)
                .padding(.horizontal, 15)
                .background(Color.white)
                .cornerRadius(9)
            .frame(height: 60)
                .padding(.horizontal, 15)
                .cornerRadius(9)
                .padding(.horizontal, 5)
            
            //Put label here to display output on button click

            Spacer()
            
            Button(action: ) 
                Text("Calculate")
                    .foregroundColor(.white)
                    .font(.system(size: 24, weight: .medium))
            .frame(maxWidth: .infinity)
                .padding(.vertical, 15)
                .background(Color.green.opacity(10))
                .cornerRadius(30)
                .padding(.horizontal, 70)
            
            Spacer()

            
        .background(Color.white)
            .edgesIgnoringSafeArea(.all)
    

//------------------------------------------------------
//Distance calculations, sources below
//geodatasource.com/developers/swift
//sisense.com/blog/latitude-longitude-distance-calculation-explained
func degreeToRadian(deg:Double) -> Double 
    return deg * Double.pi / 180


func radianToDegree(rad:Double) -> Double 
    return rad * 180 / Double.pi


func distance(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double 
    let theta = lon1 - lon2
    var dist = sin(degreeToRadian(deg: lat1)) * sin(degreeToRadian(deg: lat2)) + cos(degreeToRadian(deg: lat1)) * cos(degreeToRadian(deg: lat2)) * cos(degreeToRadian(deg: theta))
    dist = acos(dist)
    dist = radianToDegree(rad: dist)
    dist = dist * 60 * 1.1515 * 1.609344
    
    return dist

//------------------------------------------------------

struct ContentView_Previews: PreviewProvider 
    static var previews: some View 
        ContentView()
    

【问题讨论】:

您可以使用这种方法:gist.github.com/daniloc/254db4eb8e009acc2409edc2da2f18ac 或这种方法:***.com/questions/58733003/… 【参考方案1】:

您可以只在文本字段上使用键盘修饰符:

.keyboardType(.decimalPad)

文档:https://developer.apple.com/documentation/uikit/uikeyboardtype

【讨论】:

以上是关于你如何实现十进制键盘?的主要内容,如果未能解决你的问题,请参考以下文章

c# winform 如何实现16进制颜色值的转换

c语言如何实现16进制字符串转换为 base64

为 PC 实现虚拟键盘

如何确保 Android 中的数字十进制键盘?

如何实现新的 iPad Pro 键盘快捷键功能?

c语言 获取鼠标键盘事件