2012-02-04 96 views
0

有人可以帮助这个代码。我正在寻找的是我的对话框上的按钮返回一个页面,当用户按下按钮,所以我不必按手机后退按钮谢谢。对话代码错误somewere?

的java文件

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.view.View.OnClickListener; 
public class DlogActivity extends Activity { 
/** Called when the activity is first created. */ 
Dialog dialog; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
dialog = new Dialog(this); 
dialog.setContentView(R.layout.main2); 
dialog.setTitle("This is my custom dialog box"); 
dialog.setCancelable(true); 
Button b=(Button)findViewById(R.id.button1); 
@SuppressWarnings("unused")Button button01 =(Button)findViewById(R.id.btn2); 


Intent intent = new Intent(DlogActivity.this, DlogActivity.class); 
startActivity(intent); 
finish(); 



                                                                         b.setOnClickListener(new OnClickListener() { 
                                                  public void onBackPressed() { 
     dialog.cancel(); 
    // Simply Dismiss the dialog to make it close and return to back.. 
      /*What you are using is not a valid construct */ 
      } 



     public void onClick(View v) { 
      dialog.show(); 
     } 
    }); 

    } 

} 

xml.code

<Button 
    android:id="@+id/btn2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:text="Button" /> 




    <ImageView 
    android:layout_width="236dp" 
    android:layout_height="220dp" 
    android:layout_marginRight="100dp" android:background="@drawable/carsee"/> 


    <RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    </RelativeLayout> 

    </LinearLayout> 
+0

srry关于那个布局 – ravon30 2012-02-04 21:56:40

+0

你的意思是说,当用户按下一个对话框的按钮,它应该是disableard? – Lucifer 2012-02-05 03:36:30

回答

0

好像问题出在下面的代码,

dialog.setContentView(R.layout.main2); 

你不能给布局这样的,而不是你需要将布局膨胀到对话框。

+0

我尝试过,但没有运气 – ravon30 2012-02-04 23:29:13

+0

我不好找到缺少的代码 – ravon30 2012-02-26 19:13:41