2010-11-25 67 views
0

我還在將一個J2ME應用程序移植到Android,現在我的問題在於GUI。 對於我所看到的,Android的活動是偉大的,但我的J2ME充滿經典:SwitchDisplayable a la Android

public void switchDisplayable(Alert alert, Displayable nextDisplayable) { 
     Display display = getDisplay(); 
     if (alert == null) 
      display.setCurrent(nextDisplayable); 
     else 
      display.setCurrent(alert, nextDisplayable); 
} 

我不能讓每一個可顯示一個活動,所以我想和視圖替換它們。我嘗試過,但它不起作用,應用程序不會更改屏幕。

更新:

謝謝回答,而是放在人vews內的FrameLayout,仍然一無所獲。這是我測試的核心代碼,這樣你就可以檢查出來:

public class TestActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.main); 
     TextView t = (TextView)findViewById(R.id.text); // Shows "Hi" 
     showDialog(); // it just shows a dialog asking if the user wants to change screen 

    } 
    showDialog() { 
     // in OnClick()... i do the following, and here is where it fails, i tried so far:   
     TestView testv= new MarcoLoco(MFActivity.this); 
    setContentView(testv); 
     testv.invalidate(); 
    testv.requestFocus(); 
    testv.showMeSomething(); 
    } 


public class TestView extends View{ 
    private Context context; 
    TextView tv;  
    public TestView(Context context) { 
     super(context); 
     this.context=context; 
    } 
    public void showMeSomething() { 
     tv = (TextView)findViewById(R.id.tessto); // it should show "Bye" 
    } 
} 

從屏幕上的「喜」自敗的OnClick後卻沒有出現,也沒有「再見」。

爲什麼,哦,爲什麼!?

回答

0

我沒有使用Java ME任何的expirience,但是這可能會幫助您

把你瀏覽一個FrameLayout裏面,那麼你可以使用

mViewA.setVisibility(View.VISIBLE); 
mViewB.setVisibility(View.GONE); 
mViewA.requestFocus(); 

到不同勢視圖之間切換

編輯:

這是一個簡短的示例程序,可以切換2個文本視圖:

public class test extends Activity { 
    boolean showTV1 = true; 

    OnClickListener ocl = new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showTV1= !showTV1; 
      if (showTV1){ 
       tv1.setVisibility(View.VISIBLE); 
       tv2.setVisibility(View.GONE); 
       tv1.requestFocus(); 
      } else { 
       tv2.setVisibility(View.VISIBLE); 
       tv1.setVisibility(View.GONE); 
       tv2.requestFocus(); 
      } 
     } 
    }; 

    private TextView tv1, tv2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv1 = (TextView) findViewById(R.id.TextView01); 
     tv2 = (TextView) findViewById(R.id.TextView02); 

     tv1.setOnClickListener(ocl); 
     tv2.setOnClickListener(ocl); 
    } 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/frameLayout01" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:background="#0f0" 
     android:text="Hi" /> 
    <TextView android:id="@+id/TextView02" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:background="#f00" 
     android:text="Bye" android:visibility="gone"/> 
</FrameLayout> 
+0

感謝您的回答!它還沒有工作,我添加了一個代碼片段,所以你都可以看到它。 – ferostar 2010-11-25 21:05:41

+0

@Peter我添加了一個簡短的示例程序,切換2個文字瀏覽 – getekha 2010-11-25 21:48:21

0

我不知道我完全理解你的問題,而是試圖尋找ViewAnimatorViewFlipper - 也許這可以幫助

PS。只是出於好奇...你有沒有嘗試過任何自動轉換器?

0

爲什麼你自己從J2ME移植到Android,而不是使用轉換工具? 更快,無需學習和擅長與Android,無需調試和維護2個代碼庫...試試www.UpOnTek.com。謝謝。

相關問題