2012-04-25 33 views
0

我有兩個佈局,我一個接一個地膨脹它,我想解僱先前的視圖和膨脹另一個,但視圖轉移到背景,因此兩個對話框都存在。我想關閉後臺對話框。
我試過以下修復程序:關閉以前的佈局和膨脹另一

1.dialog.dismiss(); ---不工作

2.view.setVisibility(View.GONE); ---對話的標題仍然與對話保持一致。

我附上整個code.Please讓我知道如何解決這個問題。謝謝。

menuproject.java

 package com.menuproject; 

     import android.app.Activity; 
     import android.app.AlertDialog; 
     import android.os.Bundle; 
     import android.view.LayoutInflater; 
     import android.view.Menu; 
     import android.view.MenuItem; 
     import android.view.View; 
     import android.view.View.OnClickListener; 
     import android.widget.Button; 

     public class menuproject extends Activity { 
      /** Called when the activity is first created. */ 

      AlertDialog.Builder dialog; 
      AlertDialog.Builder dialog2; 
      private AlertDialog alert = null; 
      private AlertDialog alert2 = null; 
      View updatedialog; 
      View updatedialog2; 

      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 

      } 

      protected void layoutsecond() { 
       // TODO Auto-generated method stub 
       this.dialog2 = new AlertDialog.Builder(this); 
       this.dialog2.setTitle("Update"); 

       LayoutInflater layoutInflater = LayoutInflater.from(this); 
       updatedialog2 = layoutInflater.inflate(R.layout.updatedialog2, null); 

       this.dialog2.setView(updatedialog2); 
       alert2 = this.dialog2.create(); 
       this.dialog2.show(); 

      } 

      @Override 
      public boolean onCreateOptionsMenu(Menu menu) { 
       // TODO Auto-generated method stub 
       MenuItem mi = menu.add(1, 1, 1, "My Menu1"); 
       mi.setIcon(getResources().getDrawable(R.drawable.icon)); 

       return super.onCreateOptionsMenu(menu); 
      } 

      @Override 
      public boolean onOptionsItemSelected(MenuItem item) { 

       switch (item.getItemId()) { 
       case 1: 

        layoutfirst(); 

        Button b = (Button) updatedialog.findViewById(R.id.Button11); 
        b.setOnClickListener(new OnClickListener() { 

         public void onClick(View v) { 
          // TODO Auto-generated method stub 

          // updatedialog.setVisibility(View.GONE); 

          // alert.dismiss(); 

          layoutsecond(); 

         } 
        }); 

        break; 

       } 
       return super.onOptionsItemSelected(item); 
      } 

      private void layoutfirst() { 
       // TODO Auto-generated method stub 

       this.dialog = new AlertDialog.Builder(this); 
       this.dialog.setTitle("Update"); 
       // this.dialog.setMessage(""); 

       LayoutInflater layoutInflater = LayoutInflater.from(this); 
       updatedialog = layoutInflater.inflate(R.layout.updatedialog, null); 

       this.dialog.setView(updatedialog); 
       alert = this.dialog.create(); 
       this.dialog.show(); 

      } 

     } 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" 
     /> 
    </LinearLayout> 

updatedialog.xml

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ScrollView02" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:fillViewport="true"> 

<LinearLayout 
    android:id="@+id/liner1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" > 
     <requestFocus></requestFocus> 

     <Button 
      android:id="@+id/Button11" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:drawableLeft="@android:drawable/ic_menu_call" 
      android:text="Update Tel" /> 

     <Button 
      android:id="@+id/Button10" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:drawableLeft="@android:drawable/ic_menu_call" 
      android:text="Update Address" /> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

    </View> 

</LinearLayout> 
</ScrollView> 

updatedialog2.xml

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ScrollView021" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:fillViewport="true"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" > 
     <requestFocus></requestFocus> 

     <Button 
      android:id="@+id/Button01" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:drawableLeft="@android:drawable/ic_menu_call" 
      android:text="Update Mobile" /> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

    </View> 

</LinearLayout> 
</ScrollView> 

回答

0

您是否嘗試過alert.setContentView(inflated_layout)而不是dialog.setView(view_name)

+0

感謝Bharat的答覆。以上代碼無效,dialog.setContentView()方法不存在。 – 2012-04-25 09:44:00

+0

我剛剛檢查過它是否在使用alertdialog。它在那裏你正在使用的API級別。 – 2012-04-25 10:06:20

+0

AlertDialog.Builder對話框;這裏的對話框是AlertDialog.Builder .API級別是8個Android 2.2。 – 2012-04-25 10:13:10