2012-03-28 88 views
0
執行行動

我有下面的XML佈局文件,HELPME.XML從按鈕在XML佈局,通過對話框中的Android

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

    <TextView 
android:id="@+id/fVal" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:textStyle="bold" 
android:textColor="#CC0000" 
android:text="Function:" /> 


<TextView 
android:id="@+id/eVal" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textStyle="bold" 
android:textColor="#CC0000" 
android:text="Error Control:" /> 

<TextView 
android:id="@+id/hDesc" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="BLAH" /> 

<TextView 
android:layout_width="wrap_content" 
android:layout_height="12dp" 
android:text="" /> 
<Button 
android:id="@+id/closeButton" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center_vertical" 
android:text="Close" /> 
</LinearLayout> 

而且從MainFile.java位調用該XML佈局在一個對話框:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
switch (item.getItemId()) { 
case 1: 
//Toast.makeText(MainActivity.this, "Help is Here...", Toast.LENGTH_SHORT).show(); 
//openHelpWindow(); 
final Dialog d = new Dialog (MainActivity.this); 
d.requestWindowFeature(Window.FEATURE_LEFT_ICON); 
d.setContentView(R.layout.helpme); 
d.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_help); 
d.setTitle("Color Finder Help"); 
d.setCancelable(true); 

// ********************************************************** 
// EDIT - FIXED CODE 
// This closes just the Dialog window... 

     Button btn = (Button) d.findViewById(R.id.closeButton); 
      btn.setOnClickListener(new OnClickListener() { 
      @Override 
       public void onClick(View v) { 
        d.dismiss(); 
       } 
      }); 
//****************************************************************** 


d.show(); 
break; 

case 2: 
Toast.makeText(MainActivity.this, "About this app...", Toast.LENGTH_SHORT).show(); 
break; 

//case 3: 
// Toast.makeText(MainActivity.this, "Exiting app...", Toast.LENGTH_SHORT).show(); 
// System.exit(0); 
// break; 
} 

return true; 
} 

,因爲我有我的自定義的XML佈局「關閉窗口」按鈕,因爲它被顯示爲在對話窗口布局的一部分從我打開nu選項選擇...我如何創建一個將關閉對話框的按鈕的操作?

回答

0

您仍然需要重寫按鈕的onclick監聽器。然後你可以像平常一樣關閉對話框。

+0

我試圖在d.show()之上添加一個onClick監聽器,但是當我運行該程序時它只是強制關閉。我在哪裏打電話?會友善地告訴我嗎?感謝 – Jay 2012-03-28 03:58:03

+0

http://developer.android.com/guide/topics/ui/dialogs.html這顯示瞭如何添加yes和no以及onclick監聽器。轉到關於添加按鈕的部分 – Kevin 2012-03-28 04:28:26

+0

感謝您的鏈接。我已經檢查了對話框中的是,否,取消等按鈕操作。但我使用佈局中的cutom按鈕,允許用戶單擊以退出對話框。我是否更清楚一點?謝謝 – Jay 2012-03-28 04:40:09