如何将目标添加到键盘完成/去/搜索按钮?

时间:2018-05-21 07:47:27

标签: ios swift keyboard action addtarget

我想将一个动作附加到键盘上的按钮上。我不想创建自定义键盘,只处理搜索/执行/完成按钮操作。

enter image description here

谢谢!

2 个答案:

答案 0 :(得分:2)

实施textFieldShouldReturn方法class YourViewController: UIViewController, UITextFieldDelegate { //conform protocol of `UITextFieldDelegate` in any of `viewDidLoad` or `viewWillAppear` method self.textFieldYouWantToHandle.delegate = self; func textFieldShouldReturn(_ textField: UITextField) -> Bool { if textField == textFieldYouWantToHandle { //any task to perform textField.resignFirstResponder() //if you want to dismiss your keyboard } return true } } 以获取该按钮点击事件,而不管按钮的名称。

public void UPI()
    {
         Long tsLong = System.currentTimeMillis()/1000;
         String transaction_ref_id = tsLong.toString()+"UPI"; // This is your Transaction Ref id - Here we used as a timestamp -

         String sOrderId= tsLong +"UPI";// This is your order id - Here we used as a timestamp -

         Log.e("TR Reference ID==>",""+transaction_ref_id);
        Uri myAction = Uri.parse("upi://pay?pa="+sVPA+"&pn="+"Merchant%20Finance"+"&mc="+"&tid="+transaction_ref_id +"&tr="+transaction_ref_id +"&tn=Pay%20to%20Merchant%20Finance%20Assets&am="+"1.00"+"&mam=null&cu=INR&url=https://mystar.com/orderid="+sOrderId);


         PackageManager packageManager = getPackageManager();
         //Intent intent = packageManager.getLaunchIntentForPackage("com.mgs.induspsp"); // Comment line - if you want to open specific application then you can pass that package name For example if you want to open Bhim app then pass Bhim app package name - 
         Intent intent = new Intent();

         if (intent != null) {
             intent.setAction(Intent.ACTION_VIEW);
             intent.setData(myAction);
            // startActivity(intent);
             Intent chooser = Intent.createChooser(intent, "Pay with...");
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                 startActivityForResult(chooser, 1, null);
             }

         }
    }


// For onActivityResult -

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        try
        {
            Log.e("UPI RESULT REQUEST CODE-->",""+requestCode);
            Log.e("UPI RESULT RESULT CODE-->",""+resultCode);
            Log.e("UPI RESULT DATA-->",""+data);



            if(resultCode == -1)
            {

                // 200 Success

            }
            else
            {
                // 400 Failed
            }


            YourActivity.this.finish(); 


        }
        catch(Exception e)
        {
            Log.e("Error in UPI onActivityResult->",""+e.getMessage());
        }
    }

答案 1 :(得分:1)

要处理返回键,请使用委托textFieldShouldReturn

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
         // your Action According to your textfield
        return true
    }

如何更改返回键

yourTextField.returnKeyType = UIReturnKeyType.search
yourTextField.returnKeyType = UIReturnKeyType.done

Apple Enum:

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
    UIReturnKeyDefault,
    UIReturnKeyGo,
    UIReturnKeyGoogle,
    UIReturnKeyJoin,
    UIReturnKeyNext,
    UIReturnKeyRoute,
    UIReturnKeySearch,
    UIReturnKeySend,
    UIReturnKeyYahoo,
    UIReturnKeyDone,
    UIReturnKeyEmergencyCall,
    UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
}