2012-02-04 75 views
-2

我試圖讓對話框中的後退按鈕返回到原始屏幕。我不知道我是否有我需要的所有進口產品。有人能告訴我我要去哪裏嗎?對話框中的後退按鈕

Java代碼:

package my.dlog; 

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); 

    b.setOnClickListener(new OnClickListener() { 
     public void onBackPressed() { 
     Intent intent = new Intent(DlogActivity.this, DlogActivity.class); 
     startActivity(intent); 
     finish(); 
     } 

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

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="false" 
    android:minHeight="400dp" 
    android:minWidth="300dp" android:background="@drawable/mint1"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     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

接受回答請 – Mun0n 2012-02-09 17:08:51

+0

抱歉延遲onclick不工作,因爲我想,但它的現在。 – ravon30 2012-02-28 18:14:43

回答

1
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 */ 
} 

還要確保Button1的在主要佈局因爲你已經使用findViewById(R.id.button1)直接設置內容視圖

+0

所以如果我採取意圖intent =新的意圖(DlogActivity.this,DlogActivity.class);然後把dialog.cancel(); 它會告訴按鈕從對話框返回一個頁面 – ravon30 2012-02-04 20:42:54

+0

它會關閉對話框,讓你去頁面上顯示哪個對話框。如果你想回到prevous頁面..使用finish()隨着dialog.cancel – 2012-02-05 03:43:57

+0

我有dialog.cancel()'......我的壞 – ravon30 2012-02-26 19:16:06

0

那麼通常後退按鈕工作的理由沒有我們的幫助。如果你把

public void onBackPressed() { 
Intent intent = new Intent(DlogActivity.this, DlogActivity.class); 
startActivity(intent); 
finish(); 
} 

了,當你按'返回'時會發生什麼?如果這不是你想要的,那麼你會發生什麼?如果沒有錯誤,我會認爲你有必需的進口。
懸崖

+0

我只想在對話框頁面上的一個按鈕關閉時,ueser按下按鈕我不能讓它回去一旦iv按下它 – ravon30 2012-02-05 00:32:30

相關問題