当用户在android中的softkeypad中长按退格键(KEYCODE_DEL
)时,我想实现自己的方法。
到目前为止,我已完成以下操作,但它无法正常工作。
public class CustomEditText extends EditText{
private Random r = new Random();
private CustomEditText e = null;
public CustomEditText(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context) {
super(context);
}
public void setEditText()
{
e = (CustomEditText)findViewById(R.id.edit_phone_number);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return (InputConnection) new ZanyInputConnection(super.onCreateInputConnection(outAttrs),
true);
}
private class ZanyInputConnection extends InputConnectionWrapper {
public ZanyInputConnection(InputConnection target, boolean mutable) {
super(target, mutable);
}
@Override
public boolean sendKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// This is never called . So i do not know when the user pressed
// and unpressed the backspace key.
// return false;
}
return super.sendKeyEvent(event);
}
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength)
{
//This is getting called when user press and unpress the backspace
if (beforeLength >= 1 && afterLength == 0) {
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
}
当我按下softKeypad sendKeyEvent
上的退格键时,不会调用deleteSurroundingText
,但会调用KeyEvent.ACTION_DOWN
。
对于长按检测,我想在退格上获得KeyEvent.ACTION_UP event
事件,在退格上获得KeyEvent.ACTION_DOWN
,如果这两个事件之间的时差超过1/2秒,我将假设它为'长款。
由于KeyEvent.ACTION_UP
和sendKeyEvent
都来自sendKeyEvent
方法。但editPhoneNumberText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// You can identify which key pressed buy checking keyCode value
// with KeyEvent.KEYCODE_
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
// this is for backspace
Logger.log("Thhis is what i need");
}
return false;
}
});
从未被调用过。
所以我不知道我该怎么做。
如果您有任何其他方法,请提供帮助。
<%@ Page Title="" Language="C#" MasterPageFile="~/home/MasterPage.master" AutoEventWireup="true" CodeFile="Letter_LetterData.aspx.cs" Inherits="DataManagement_Letter_LetterData" %>
<%@ Register Src="~/Controls/ResultLabel.ascx" TagPrefix="uc1" TagName="ResultLabel" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script type="text/javascript">
function Clear() {
location.reload(true);
}
function GetConfrim() {
if (confirm("Are You Sure ?")) {
return true;
} else {
return false;
}
}
function isNumberKey_txtBoxApplicantContactNo(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 43) {
return false;
}
return true;
}
function isNumberKey_txtBoxApplicantCNIC(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 45) {
return false;
}
return true;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:UpdateProgress AssociatedUpdatePanelID="UpdatePanel1" runat="server" ID="UpdateProgress1">
<ProgressTemplate>
<div class="ajax-loading">
<div></div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="page-content">
<div class="row">
<div class="col-lg-12">
<center>
<asp:Panel ID="ResultPanel" runat="server">
</asp:Panel>
</center>
</div>
<div class="col-md-12">
<div class="panel">
<div class="panel-header bg-primary">
<h3><i class="fa fa-book"></i>Create Letter</h3>
</div>
<div class="panel-content">
<%--Row_1 Start --%>
<asp:HiddenField ID="HdnFieldRecievedLetterID" runat="server" />
<div class="col-md-12">
<div class="col-md-3">
<div class="form-group">
<label class="control-label">
Letter NO.
<asp:HiddenField ID="HdnLetterNumber" Value='<%#Eval("Letter_No") %>' runat="server" />
</label>
<asp:TextBox runat="server" ID="txtBoxLetterNo" CssClass="form-control"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">
Subject
<asp:HiddenField ID="HdnfldltrSubject" Value='<%#Eval("Subject_ltr") %>' runat="server" />
</label>
<asp:TextBox runat="server" ID="txtltrSubject" CssClass="form-control"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Sender Designation</label>
<asp:DropDownList runat="server" ID="ddlDesignation" CssClass="form-control form-white "></asp:DropDownList>
<asp:HiddenField ID="HdnReceivedByDesignation" Value='<%#Eval("SenderDesignation_ID") %>' runat="server" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">City</label>
<asp:DropDownList runat="server" ID="ddlCity" CssClass="form-control form-white"></asp:DropDownList>
</div>
</div>
</div>
<%--Row_1 End--%>
<%-- Row_2 Start--%>
<div class="col-md-12">
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Department</label>
<asp:DropDownList runat="server" ID="ddlDepartment" CssClass="form-control form-white "></asp:DropDownList>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">
Issue Date
<asp:HiddenField ID="HdnIssueDate" runat="server" />
</label>
<asp:TextBox runat="server" ID="txtIssueDate" CssClass="form-control" placeholder="dd/mm/yyyy"></asp:TextBox>
<asp:CalendarExtender ID="cal1" runat="server" Format="dd/MMM/yyyy" TargetControlID="txtIssueDate"></asp:CalendarExtender>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">
Recieved Date
<asp:HiddenField ID="HdnRecievedDate" runat="server" />
</label>
<asp:TextBox runat="server" ID="txtRecievedDate" CssClass="form-control" placeholder="dd/mm/yyyy"></asp:TextBox>
<asp:CalendarExtender ID="Cal2" runat="server" Format="dd/MMM/yyyy" TargetControlID="txtRecievedDate"></asp:CalendarExtender>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Priority ID</label>
<asp:DropDownList runat="server" ID="ddlPriority" CssClass="form-control form-white"></asp:DropDownList>
</div>
</div>
</div>
<%--Row_2 End--%>
<%-- Row_3 Start--%>
<div class="col-md-12">
<div class="col-md-3">
<div class="form-group">
<label class="control-label">
Priortized Time/Date
<asp:HiddenField ID="HdnPriortizedDateTime" runat="server" />
</label>
<asp:TextBox runat="server" ID="txtPriortizedDateTime" CssClass="form-control" placeholder="dd/mm/yyyy"></asp:TextBox>
<asp:CalendarExtender ID="Cal3" runat="server" Format="dd/MMM/yyyy" TargetControlID="txtPriortizedDateTime"></asp:CalendarExtender>
</div>
</div>
<asp:HiddenField ID="HdnReceivedByName" Value='<%#Eval("User_ID") %>' runat="server" />
<%-- <div class="col-md-3">
<div class="form-group">
<label class="control-label">User Name</label>
<asp:DropDownList runat="server" ID="ddlUserName" CssClass="form-control form-white "></asp:DropDownList>
</div>
</div>--%>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">
Sender Name
</label>
<asp:TextBox runat="server" ID="txtSenderName" CssClass="form-control"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Letter Type</label>
<asp:DropDownList runat="server" ID="ddlLetterType" CssClass="form-control form-white"></asp:DropDownList>
</div>
</div>
</div>
<%--Row_3 End--%>
<div class="clearfix"></div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<input type="button" value="Reset" onclick="Clear()" class="btn btn-success" />
<asp:Button ID="btnAdd" ValidationGroup="ValGroupApplicants" runat="server"
CausesValidation="true" CssClass="btn btn-primary" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnUpdate" runat="server" CssClass="btn btn-primary"
Text="Update" Visible="false" OnClick="btnUpdate_Click" />
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel fadeIn">
<div class="panel-header bg-success">
<h3><i class="fa fa-search"></i>Letter Details</h3>
</div>
<div class="panel-content">
<div class="col-md-4 ">
<div class="input-group">
<asp:TextBox runat="server" CssClass="form-control" ID="txtSearch" PlaceHolder="Letter Subject"></asp:TextBox>
<span class="input-group-btn">
<asp:Button runat="server" ID="btnSearch" CssClass="btn btn-warning" Text="Search" ValidationGroup="S" OnClick="btnSearch_Click" />
</span>
</div>
</div>
<div style="overflow-x:auto;width:970px"> <%--This <Div> is used for Horizontel scrolling of GridView--%>
<asp:GridView runat="server" ID="grdview" OnRowCommand="grdview_RowCommand" DataKeyNames="ReceivedLetter_ID" AutoGenerateColumns="false"
CssClass="table table-condensed table-bordered table-striped table-responsive scrollable" HeaderStyle-BackColor="#99CC99">
<Columns>
<asp:BoundField DataField="Letter_No" HeaderText="Letter Number" />
<asp:BoundField DataField="Subject_ltr" HeaderText="Subject" />
<asp:BoundField DataField="SenderDesignation_ID" HeaderText="Sender Designation" />
<asp:BoundField DataField="City_ID" HeaderText="City" />
<asp:BoundField DataField="Department_ID" HeaderText="Department ID" />
<asp:BoundField DataField="IssueDate" HeaderText="Issue Date" />
<asp:BoundField DataField="Received_Date" HeaderText="Received Date" />
<asp:BoundField DataField="Priority_ID" HeaderText="Priority ID" />
<asp:BoundField DataField="PrioritizedDatetime" HeaderText="Prioritized Datetime" />
<asp:BoundField DataField="User_ID" HeaderText="User ID" />
<asp:BoundField DataField="Sender_Name" HeaderText="Sender Name" />
<asp:BoundField DataField="LetterType_ID" HeaderText="LetterType ID" />
<asp:ButtonField CommandName="cmdEdit" ImageUrl="~/assets/global/images/shopping/Edit1.jpg" HeaderText="Edit" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
<asp:TemplateField HeaderText="Move Letter" >
<ItemTemplate>
<a href ='<%#"Letter_Movement.aspx?ReceivedLetterId="+DataBinder.Eval(Container.DataItem,"ReceivedLetter_ID") + "&LetterNumber=" +HdnLetterNumber.Value + "&LetterSubject=" +HdnfldltrSubject.Value + "&ReceivedByName=" +HdnReceivedByName.Value + "&ReceivedByDesignation=" +HdnReceivedByDesignation.Value %>'> <img src="../assets/global/images/shopping/Move.png" alt="Ltter Move" style="height:50px;width:50px;" /> </a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/assets/global/images/shopping/Delete_.jpg" CommandName="cmdDelete" CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm('Are you Sure ?');" ControlStyle-Width="25px" ControlStyle-Height="20px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUpdate" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
答案 0 :(得分:3)
我遇到了类似的问题。只需更改以下功能,其余的就可以了: -
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
if(android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
if (beforeLength >= 1 && afterLength == 0) {
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
}
else if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
if (beforeLength == 1 && afterLength == 0) {
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
希望它有所帮助。