2012-02-01 15 views
4

私はEditTextウィジェットを使用しており、ユーザーが長時間ビューを押したときに表示されるコンテキストメニューを変更したいと考えています。私が抱えている問題は、長押しのテキスト内の文字の位置を知る必要があるため、コンテキストメニューに何を追加する必要があるかを判断できるということです。メニューの選択肢の1つが「追加」word_clicked_on「To Dictionary」であるため、基本クラスでこれが行われます。テキスト内のClickableSpansを設定することは、クリックイベントを消費するため、スパン内で編集カーソルを移動することができないため、解決策としては表示されません。ここで長押しの位置に基づいてEditTextの文脈/長押しメニューを動的に変更する

答えて

4

は私が思い付いた、それは私がそれを共有したいと思っ動作しません解決策は以下のとおりです。

まず私はonTouchEventを傍受することができるように私はのEditTextクラスを拡張する必要があると結論付け、ACTION_DOWNをキャプチャイベントを保存し、その位置を保存します。これで、私はgetOffsetForPosition(downPointX、downPointY)を呼び出して、長押しの文字位置を取得できるダウンポイントの位置を取得しました。大きな問題が1つあります.getOffsetForPositionはSDK 14まで追加されていませんでした。私は現在のSDKがここSDK_INT 14よりも前である場合は、ポートにgetOffsetForPositionと分岐の機能をバックアップする必要がありました。このソリューションを機能させるためには、新しいクラスのソースコードである:あなたの活動派生クラスで

public class ScrapEditText extends EditText{ 

protected float LastDownPositionX, LastDownPositionY; 

public ScrapEditText(Context context) 
{ 
    super(context); 
} 

public ScrapEditText(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    final int action = event.getActionMasked(); 
    if(action == MotionEvent.ACTION_DOWN) 
    { 
     LastDownPositionX = event.getX(); 
     LastDownPositionY = event.getY(); 
    } 
    return super.onTouchEvent(event); 
} 

public float GetLastDownPositionX() 
{ 
    return LastDownPositionX; 
} 

public float GetLastDownPositionY() 
{ 
    return LastDownPositionY; 
} 

public int GetOffsetForLastDownPosition() 
{ 

    if(Build.VERSION.SDK_INT > 13) 
    { 
     // as of SDK 14 the getOffsetForPosition was added to TextView 
     return getOffsetForPosition(LastDownPositionX, LastDownPositionY); 
    } 
    else 
    { 
     return GetOffsetForPositionOlderSdk(); 
    } 
} 

public int GetOffsetForPositionOlderSdk() 
{ 
    if (getLayout() == null) return -1; 
    final int line = GetLineAtCoordinateOlderSDK(LastDownPositionY); 
    final int offset = GetOffsetAtCoordinateOlderSDK(line, LastDownPositionX); 
    return offset; 
} 

public int GetLineAtCoordinateOlderSDK(float y) 
{ 
    y -= getTotalPaddingTop(); 
    // Clamp the position to inside of the view. 
    y = Math.max(0.0f, y); 
    y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y); 
    y += getScrollY(); 
    return getLayout().getLineForVertical((int) y);  
} 

protected int GetOffsetAtCoordinateOlderSDK(int line, float x) { 
    x = ConvertToLocalHorizontalCoordinateOlderSDK(x); 
    return getLayout().getOffsetForHorizontal(line, x); 
} 

protected float ConvertToLocalHorizontalCoordinateOlderSDK(float x) { 
    x -= getTotalPaddingLeft(); 
    // Clamp the position to inside of the view. 
    x = Math.max(0.0f, x); 
    x = Math.min(getWidth() - getTotalPaddingRight() - 1, x); 
    x += getScrollX(); 
    return x; 
} 

} 

ScrapText = (ScrapEditText) findViewById(R.id.sample_text); 
ScrapText.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){ 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
{ 
    int charOffset = FileText.GetOffsetForLastDownPosition(); 
}   
}); 
関連する問題