2016-04-25 10 views
0

私は9 * 9のキーボードモーダルウィンドウを持っています。そしてそれは数独ゲームボードを埋めるために使用されました。アンドロイドスタジオで 、 は、ボタンの位置を取得するために、私は彼がキーを選択した後、この希望の場所にモデルウィンドウを表示するには?

int offsetX=30; 
int offsetY=30; 
_popupWindow.showAtLocation(_keyBoardLayout, Gravity.NO_GRAVITY, _p.x+offsetX, _p.y+offsetY); 

よう

_selectedButton=(Button)view; 

     int tag=(int)_selectedButton.getTag(); 
     _currentRow=tag/9; 
     _currentCol=tag%9; 
int[] location=new int[2]; 
     _selectedButton.getLocationOnScreen(location); 
     _p=new Point(); 
     _p.x=location[0]; 
     _p.y=location[1]; 

     ShowKeyBoard(); 

ShowKeyBoardがある次のコードを使用して、私はポップアップウィンドウを閉じます。

public void BtnKey1Pressed(View view) 
    { 
     _selectedButton.setText("1"); 
     _popupWindow.dismiss(); 
    } 

どうすればXamarin Androidで行うことができますか? xamarinでこのような返信データを取得することは可能ですか?

int selectedKey=ShowKeyBoard(); 
+0

のようなあなたのフィールドの数を得ることができ、あなたの質問を指定してくださいでした。 ShowKeyboardから戻り値を取得するには、戻り値で宣言して値を返す必要があります; – Matt

+0

Javaでアンドロイドスタジオを使用して作成したすべてのコードをXamarin android形式で使用します。 –

答えて

2

ポップアップウィンドウの迅速かつ汚れた実装を作成しようとしました。私はあなたが今クリックしたボタンにあなたのポップアップを表示したいと思っていたので、ShowAsDropDownを使ったのです。私はGetLocationOnScreenコードを残しました。例えば、それを渡すだけです。これにより

public sealed class MyPopup : PopupWindow 
{ 
    private readonly Action<int> _callbackMethod; 

    private MyPopup(Activity context, Action<int> callbackMethod) 
     : base(context.LayoutInflater.Inflate(Resource.Layout.Popup, null), 
      ViewGroup.LayoutParams.WrapContent, 
      ViewGroup.LayoutParams.WrapContent) 
    { 
     _callbackMethod = callbackMethod; 
    } 

    public static Task<int> GetNumber(Activity mainActivity, Button button) 
    { 
     var t = new TaskCompletionSource<int>(); 
     var popupWindow = new MyPopup(mainActivity, i => t.TrySetResult(i)); 
     popupWindow.Show(button); 
     return t.Task; 
    } 

    private void Show(View anchor) 
    { 
     SetActionForChildButtons(anchor, View_Click); 
     ShowAsDropDown(anchor); 
    } 

    private void SetActionForChildButtons(View parent, EventHandler e) 
    { 
     var button = parent as Button; 
     if (button != null) 
     { 
      button.Click += e; 
      return; 
     } 

     var viewGroup = parent as ViewGroup; 
     if (viewGroup == null) 
      return; 

     for (var i = 0; i < viewGroup.ChildCount; i++) 
     { 
      var view = viewGroup.GetChildAt(i); 
      SetActionForChildButtons(view, e); 
     } 
    } 

    private void View_Click(object sender, EventArgs e) 
    { 
     var button = sender as Button; 
     if (button == null) 
      return; 

     int number; 
     if (int.TryParse(button.Text, out number)) 
      _callbackMethod?.Invoke(number); 

     Dismiss(); 
    } 
} 

あなたは何とかこの

private async void OnClick(object sender, EventArgs e) 
{ 
    var button = sender as Button; 
    if (button == null) 
     return; 
    var location = new int[2]; 
    button.GetLocationOnScreen(location); 
    var number = await MyPopup.GetNumber(this, button); 
    button.Text = number.ToString(); 
} 
+0

アイデアありがとう。上のコードは私のために働いていませんが、私は上記の答えと同様のものを作成しました。私は1つの問題を抱えていますが、最後の行のボタンをクリックすると、範囲外であるためポップアップが表示されません。ポップアップが外れてしまう場合は、下の代わりに上に表示するにはどうすればよいですか? –

+0

ポップアップの位置と実際の高さがあるので、location + heightがレイアウトの高さよりも大きいかどうかを確認することを提案します。しかし、あなたはおそらくShowAtLocation – Matt

関連する問題