2012-01-31 38 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