2016-03-04 52 views
-3

编辑 - 更具体,我根据的评论尝试,并没有达到我的目标编辑试图使图书馆在JS与OOP

所以我想做一个GUI库的ModPE脚本编写者。而不是他们打字它所有我希望它被预先定义和组织在课堂上。所以我做了所有的思考和解决,他们可以更容易地创建GUI模块。我固定并尝试...

var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get(); 
function newLevel() 
{ 
    ctx.runOnUiThread(new java.lang.Runnable() { 
     run: function() 
     { 
      try 
      { 
       var PopupsAndLayouts = { 
        PopupWindow: new android.widget.PopupWindow(ctx), 
        LinearLayout: new android.widget.LinearLayout(ctx), 
           Button: new android.widget.Button(ctx); 
        wrapContect: android.widget.LinearLayout.LayoutParams.WRAP_CONTENT; 
        fillParent: android.widget.LinearLayout.LayoutParams.FILL_PARENT; 
        leftTopCorner: [ctx.getWindow().getDecorView(), android.view.Gravity.TOP | android.view.Gravity.LEFT, 0, 0] 
       }; 
       var myWindow = PopupsAndLayouts.PopupWindow; 
       var myLayout = PopupsAndLayouts.LinearLayout; 
       var myButton = PopupsAndLayouts.Button; 
       myLayout.addView(myButton); 
       myWindow.setContentView(myLayout); 
       myWindow.setHeight(wrapContent); 
       myWindow.setWidth(wrapContent); 
       myWindow.showAtLocation(leftTopCorner); 
       //Then they add more widgets or set their script 
      } 
      catch (libError) 
      { 
       print(error); 
      } 
     } 
    }); 
} 

但它不会工作。点是组织类,所以他们不必知道 var myButton = new android.widget.Button(ctx); 或 var myLayout = new android.widget.LinearLayout(ctx);

他们只是键入 var myLayout = Layouts.LinearLayout; 或 var myButton = Widgets.Buttons.Button;

我该怎么做到这一点?我迷路了,想要建立这个图书馆的任何想法?

怎么样GridLayout?尽管一般来说不是OOP明智的。

+0

'var PopupsAndLayouts = {var'全都是错的... – dandavis

+0

那我该如何解决呢? –

+0

正如我之前所说,我不会在Js –

回答

1

对于网格布局,你可以这样来做

var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get(); 
function newLevel() 
{ 
    ctx.runOnUiThread(new java.lang.Runnable() { 
     run: function() 
     { 
      try 
      { 
       var l = new android.widget.GridLayout(ctx); 
       var b1 = new android.widget.Button(ctx); 
       var b2 = new android.widget.Button(ctx); 
       var b3 = new android.widget.Button(ctx); 
       var ba = [b1, b2, b3]; 
       for (int i = 0; i < ba.length; i++) 
       { 
        bl.addView(ba[i]); 
       } 
       var p = new android.widget.PopupWindow(l, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT); 
       p.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.CENTER | android.view.Gravity.CENTER, 0, 0); 
      } 
      catch (defaultError) 
      { 
       print(defaultError); 
     } 
    }); 
} 

for循环提供了灵活性,但如果你不喜欢那么你可以这样做只是增加他们喜欢....

l.addView(b1, b2, b3); 
0

这看起来更像JavaScript ...但我不知道面向对象,所以我不能说我有正确的答案。

var PopupsAndLayouts = { 
    PopupWindow: new android.widget.PopupWindow(ctx), 
    LinearLayout: new android.widget.LinearLayout(ctx), 
    Button: new android.widget.Button(ctx), 
    wrapContent: android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, 
    fillParent: android.widget.LinearLayout.LayoutParams.FILL_PARENT, 
    leftTopCorner: [ctx.getWindow().getDecorView(), android.view.Gravity.TOP | android.view.Gravity.LEFT, 0, 0] 
}; 
+0

中做太多的课程和对象啊,好吧......所以我没有做错课。是的,它是javascript rhino中的Java(Rhino)Java –