2012-01-31 36 views
1

我浪費了很多時間試圖寫:走動作爲單獨的類

private void showAbout() { 
    Dialog dialog = new Dialog(Generator.this); 
    dialog.setContentView(R.layout.about); 
    dialog.setTitle(getString(R.string.about)); 
    dialog.setCancelable(true); 

    try { 
     TextView tv_version = (TextView) dialog.findViewById(R.id.tv_version); 
     tv_version.setText("Version number: " + getPackageManager().getPackageInfo(
      getPackageName(), 0).versionName); 
     TextView tv_createdBy = (TextView) dialog 
        .findViewById(R.id.tv_createdBy); 
      tv_createdBy.setText(getString(R.string.made_by)); 
     } catch (NameNotFoundException e) { 
      Log.e(TAG, "showAbout()", e); 
     } finally { 
      dialog.show(); 
     } 
} 

在在希望的一類,使我的代碼更易讀。

我寫這樣的:

private void showAbout() { 
       About about = new About(); 
       about.show(); 
} 

public class About extends Activity { 
    String TAG = "About"; 
    Dialog dialog; 

    /** 
    * 
    */ 
    public About() { 
     dialog = new Dialog(About.this); 

    } 

    public void show() { 

     dialog.setContentView(R.layout.about); 
     dialog.setTitle(getString(R.string.about)); 
     dialog.setCancelable(true); 

     try { 
      TextView tv_version = (TextView) dialog 
        .findViewById(R.id.tv_version); 
      tv_version 
        .setText("Version number: " 
          + getPackageManager().getPackageInfo(
            getPackageName(), 0).versionName); 
      TextView tv_createdBy = (TextView) dialog 
        .findViewById(R.id.tv_createdBy); 
      tv_createdBy.setText(getString(R.string.made_by)); 
     } catch (NameNotFoundException e) { 
      Log.e(TAG, "showAbout()", e); 
     } finally { 
      dialog.show(); 

     } 
    } 

} 

的Bt這是行不通的。它似乎是在創建對話框時崩潰的,但我不知道如何以另一種方式編寫它。

任何想法?

+0

你怎麼稱呼這項活動?你有沒有在androidmanifest.xml中定義活動? – ariefbayu 2012-01-31 13:52:52

+0

這是一個很好的觀點。您如何確定如何定義活動?我並不擅長這一點 - 但是。 – ringkjob 2012-01-31 18:15:26

+0

你想如何顯示'about'?在彈出對話框?或在另一個活動? – ariefbayu 2012-02-01 01:17:08

回答

0

鑑於您希望以pop-up的方式顯示。那麼你做錯了。將類擴展到Activity將使其成爲活動類(用於活動)。您應該這樣做:

//class level variable 
private Dialog formDialog = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    prepareDialog(); 
} 

/** 
* We prepare dialog in it's own method so that we can maintain code separation well. 
* I believe there is another <em>better</em> solution. But, it work for now. 
*/ 
private void prepareDialog() { 
    formDialog = new Dialog(ListDataActivity.this); 
    formDialog.setContentView(R.layout.form); 
    formDialog.setTitle("Form Buku"); 

    // set dialog width to fill_parent 
    LayoutParams formDialogParams = formDialog.getWindow().getAttributes(); 
    formDialogParams.width = LayoutParams.FILL_PARENT; 
    formDialog.getWindow().setAttributes(
      (android.view.WindowManager.LayoutParams) formDialogParams); 

    txtNama = (EditText) formDialog.findViewById(R.id.txtFormNamaPenulis); 
    txtNama.setText("about info"); 


    btnSimpan = (Button) formDialog.findViewById(R.id.btnFormSimpan); 

    btnBack.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      formDialog.hide(); 
     } 
    }); 
} 

請注意,您還需要爲此對話框創建新的.xml佈局。

最後,您只需撥打formDialog.show();即可顯示。這個答案從我的android tutorial中提取並略微修改。

0

我見過的關於擴展Activity類的每個示例都包括覆蓋onCreate方法。因此,您應該添加onCreate方法並將其稱爲超級方法super.onCreate(savedInstanceState);

我也認爲顯示的對話框不應該是一個活動。對話總是活動的一部分。

0

你可以把代碼創建並顯示在基地Activity「關於」對話框,然後讓你的其他Activites延伸基地之一。

+0

我不太清楚你的意思。一般來說,你認爲我應該放棄它,因爲這只是一個壞主意? – ringkjob 2012-01-31 18:47:47