Dialog

Dialog位置調整

1.ダイアログを呼び出すときに、デフォルト位置はスクリーンの真ん中となるが位置を調整する場合は、下記の通りです。

1
2
3
4
5
6
Window window=dlg.getWindow();
WindowManger.LayoutParams lp=window.getAttributes();
lp.x=10;
lp.y=150;
window.setAttributes(lp);
dlg.show();

上記のPoint「lp.x/lp.y」は、ダイアログウィンドウの中心ポイントです。左上ポイントではありません。 window.setGravity(Gravity.CENTER);

1
window.setGravity(Gravity.TOP|Gravity.LEFT);

左上ポイントで計算したい場合は、上記のGravityを設定すれば良いと思いますが
Android2.2ではどうしても動かないと、CENTENで移さなければいけないようです。

2.カスタマイズダイアログを作るときに、子クラスのshowメソッドを実装して、左上のポイント座標を渡されて実現は、下記になります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public void show(int topLeftPointX, int topLeftPointY) {
//真ん中ポイントに変える↓
    int dialogCenterPointX = topLeftPointX + pxDialogWidth / 2;
    int dialogCenterPointY = topLeftPointY + pxDialogHeight / 2;
//真ん中ポイントに変える↑
    Window window = this.getWindow();
    WindowManager m = getWindow().getWindowManager();
    Display d = m.getDefaultDisplay();
    int screenWidth = d.getWidth();
    int screenHeight = d.getHeight();
    WindowManager.LayoutParams lp = window.getAttributes();
//[screenWidth / 2]と[screenHeight / 2]はスクリーンの真ん中ポイント
    lp.x = dialogCenterPointX - screenWidth / 2;
    lp.y = dialogCenterPointY - screenHeight / 2;
    window.setGravity(Gravity.CENTER);
    window.setAttributes(lp);
    show();
    }

Android2.2では、真ん中ポイントしか動けないので、渡された左上のポイントを変える必要です 加算したウィンドウの真ん中ポイントとスクリーンの真ん中ポイントと合算して新しい真ん中ポイントを計算して 設定する。

ProgressDialogの使用説明

◆ProgressDialogの使用方法を説明します

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class SetupActivity extends Activity implements Runnable{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.main);
          }
         private final Handler=new Handler(){
             @Override
             public void handlerMessage(Message msg){
//メッセージを受け取ると、ProgressDialogを閉じる
                   progressDialog.dismiss();
             }
          }
         private void startSetup(){
                 progressDialog=new ProgressDialog(this);
                 progressDialog.setMessage(getString(R.string.dialog_message_progress_wait));
//ProgressDialogを表示する
                 progressDialog.show();
//タスクのスレッドを起動する
                 Thread.thread=new Thread(this);
                 thread.start();
         }
         @Override
         public void run(){
               //TODO タスクの処理
//タスクの処理を終わると、Handlerでメッセージを送信して、ProgressDialogを閉じる
              Message message=new Message();
              message.that=0;
              handler.sendMessage(message);
         }
}

InputTextDialogに鍵盤で入力する場合、鍵盤表示位置問題解決すること

①エミュレータのソフトキーボードがダイアログに隠れてしまう 直接にAlertDialogから継承されるCasioBaseDialogを使えない、このAlertDialogのBuilderを使えば、この問題を解決できる 下記のようにBuilderで新たなAlertDialogを作る

1
2
3
4
5
alertDialog=new AlertDialog.Builder(context).create();
alertDialog.setView(getLayoutInflater().inflater(R.layout.dialog_input,null));
//Get the view of the showing alertDialog
View view =alertDialog.getWindow().getDecorView();
alertDialog.setContentView(getLayoutInflater().inflate(R.layout.dialot_input,null))

このまま運行すれば、このDialogが親のAlertDialogに隠れてしまう 下記のように親のAlertDialogのshow()メソッドをオーバーライド、dismiss()でこのAlertDialogを消える

1
2
3
4
5
@Override
public void show(){
      super.show();
      this.dismiss();
}

②入力区域フールスクリーン エミュレータは横画面の場合、入力領域は自動的にフールスクリーンしている 下記のように「 android:imeOptions="flagNoExtractUi"」を使えば、フールスクリーンをキャンセルする

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<EditText
  android:id="@+id/editTextInputDialogContent"
  android:layout_height="250px"
  android:layout_x="12px"
  android:layout_y="51px"
  android:textSize="20sp"
  android:textColor="@android:color/black"
  android:gravity="center"
  android:imeOptions="flagNoExtractUi"
  android:layout_width="365px"
  android:background="@null"/>

③サブクラス中親クラスの関数を使用する場合 親クラスで下記のように関数を定義する

1
2
3
public void setCalledByViewId(int viewId){
   this.calledByViewId=viewId;
}
1
2
3
4
5
this.textButtonLeft=textButtonLeft;
this.textButtonRight=textButtonRight;
this.oldValue=oldValue;
this.maxLength=maxLength;
setCalledByViewId(calledByViewId);//サブクラスで親クラスの[calledByViewId]を利用する

Androidアプリで、ポップアップ・ウィンドウの起動方法は下記通りです

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    PopupWindow pw;
    private int popWidth;
    private int popHeight;
    private int offsetX;
    private int offsetY;
    private View parentView;
//ポップアップ・ウィンドウの幅を設定する
    this.popWidth = 100dp;
//ポップアップ・ウィンドウの高さを設定する
    this.popHeight = 400dp;
//ポップアップ・ウィンドウを画面で表示するX位置を設定する
    this.offsetX = x;
//ポップアップ・ウィンドウを画面で表示するY位置を設定する
    this.offsetY = y;
//ポップアップ・ウィンドウの父ビューを設定する
    this.parentView = parent;
//ポップアップ・ウィンドウの内容ビューを設定する
    if (view == null) {    
        view = inflater.inflate(R.layout.popup, null, false);
    }
//内容ビューを表示することができる新しいポップアップ・ウィンドウを作成する
    pw = new PopupWindow(view, this.popWidth, this.popHeight, true);
//指定された位置でポップアップ・ウィンドウを表示する
    pw.showAtLocation(this.parentView, Gravity.RIGHT, this.offsetX,this.offsetY);

参考ソース:InDrawer841(在高登録)