打开对话框绑定到另一个上下文

时间:2013-05-02 13:09:51

标签: android dialog

我有一个Activity(DocumentActivity.java),带有一个带有对话框的有界上下文(documents_link_onclick_dialog.xml)。在这个对话框中,当我点击一个按钮时,我想打开另一个对话框(documents_link_editlink.xml),但这次对话框应该有另一个Activity(UrlActivity.java)。 我尝试了这4个,但没有一个工作。

// Try 1
new UrlActivity().showDialog(R.layout.documents_link_editlink);

// Try 2
startActivity(new Intent(this, UrlActivity.class));

// Try 3
dialog.setContentView(R.layout.documents_link_editlink);
dialog.show();

// Try 4
final Dialog dialog2 = new Dilog(UrlAcitivty.this);
dialog2.show()

我做错了什么? 我希望有人可以帮助我:)。

在这里您可以找到相关的课程:

的AndroidManifest.xml

<activity
    android:name=".documents.UrlActivity"
    android:label="@string/documents_link_edit_activity_title"
    android:theme="@android:style/Theme.Black"
    > 
    <intent-Filter>
        <action android:name="de.hsaugsburg.hsapp.documents.intent.action.view" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-Filter>
</activity>

DocumentActivity.java:

public class DocumentActivity extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        refreshList();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null && data.getAction().equals("ACTION_REFRESH")) {
            refreshList();
        }
    }

    private void initLinkOptionMenu(final Link selected) {
        final Dialog dialog = new Dialog(DocumentActivity.this);

        dialog.setContentView(R.layout.documents_link_onclick_dialog);
        dialog.setTitle("Choose action...");

        final Button buttonOpen = (Button) dialog.findViewById(R.id.dlol_buttonOpen);
        buttonOpen.setText("Open Link!");
        final ImageView imageOpen = (ImageView) dialog.findViewById(R.id.dlol_imageOpen);
        imageOpen.setImageResource(R.drawable.ic_save);
        buttonOpen.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        closeDialog(dialog);
                        openLink(selected);
                    }
                });

        final Button buttonEdit = (Button) dialog.findViewById(R.id.dlol_buttonEdit);
        buttonEdit.setText("Edit Link!");
        final ImageView imageEdit = (ImageView) dialog.findViewById(R.id.dlol_imageEdit);
        imageEdit.setImageResource(R.drawable.ic_edit);
        buttonEdit.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        closeDialog(dialog);
                        initEditDialog(dialog);
                    }
                });

        final Button buttonShare = (Button) dialog.findViewById(R.id.dlol_buttonShare);
        buttonShare.setText("Share Link!");
        final ImageView imageShare = (ImageView) dialog.findViewById(R.id.dlol_imageShare);
        imageShare.setImageResource(R.drawable.ic_share2);

        final Button dialogButton = (Button) dialog.findViewById(R.id.dlol_dialogButtonOK);
        dialogButton.setText("Close");
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closeDialog(dialog);
            }
        });

        dialog.show();
    }
    private void initEditDialog(final Dialog dialog) {
        new UrlActivity().showDialog(R.layout.documents_link_editlink);
        LOG.info("URLActivity started: ");
        startActivity(new Intent(this, UrlActivity.class));

        dialog.setContentView(R.layout.documents_link_editlink);
        dialog.show();

        final Dialog dialog2 = new Dilog(UrlAcitivty.this);
        dialog2.show()
    }

    private void refreshList() {
        try {

            final AsyncTask<String, Void, String> result = Requester
                    .requestCategory(getBaseContext());

            final String res = result.get();

            if (res != null) {
                categories = InputManager.parseCategories(res);

                setContentView(R.layout.documents_main);

                tabs = (TabHost) findViewById(android.R.id.tabhost);
                tabs.clearAllTabs();

                for (final Category category : categories) {
                    final TabHost.TabSpec spec = tabs.newTabSpec(category
                            .getName());

                    spec.setIndicator(category.getName());

                    spec.setContent(new TabHost.TabContentFactory() {
                        public View createTabContent(String tag) {
                            final ListView list = new ListView(
                                    DocumentActivity.this);

                            final String[] links = new String[category
                                    .getLinks().size()];
                            int i = 0;
                            for (Link link : category.getLinks()) {
                                links[i++] = link.getName();
                            }

                            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                                    DocumentActivity.this,
                                    R.layout.documents_listitem, links);

                            list.setAdapter(adapter);
                            list.setOnCreateContextMenuListener(DocumentActivity.this);
                            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                                    final Link selected = category.getLinks()
                                            .get(i);

                                    /*
                                     * Defines several behaviours for each link.
                                     */

                                    initLinkOptionMenu(selected);
                                }
                            });
                            return list;
                        }
                    });
                    tabs.addTab(spec);
                }
            } else {
                Toast.makeText(getApplicationContext(),
                        R.string.cannot_receive_from_server, Toast.LENGTH_LONG)
                        .show();

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.layout.documents_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final Intent intent = new Intent();
        switch (item.getItemId()) {
        case (R.id.menu_home):
            intent.setClass(DocumentActivity.this, de.hsaugsburg.hsapp.HSAppActivity.class);


            break;
        case (R.id.menu_addUrl):
            // this.addURL();
            intent.setClass(DocumentActivity.this, de.hsaugsburg.hsapp.documents.AddUrlActivity.class);

            final int tabID = tabs.getCurrentTab();
            final Category selected = categories.get(tabID);

            intent.setData(Uri.parse("content://category/" + selected.getID()));

            break;

        case (R.id.menu_Options):
            intent.setClass(DocumentActivity.this, de.hsaugsburg.hsapp.documents.OptionsActivity.class);
            break;

        }
        startActivityForResult(intent, 0);
        return true;
    }

    private void closeDialog(Dialog dialog) {
        dialog.dismiss();
    }
}

UrlActivity.java

public class UrlActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LOG.info("UrlActivity onCreate was created!");

        this.dialogContext = UrlActivity.this;

        final Dialog dialog = new Dialog(this.dialogContext);
        final Button buttonCancel = (Button) dialog.findViewById(R.id.documents_link_edit_cancel);
        buttonCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closeDialog(dialog);            
            }
        });

        dialog.show();

    }
}

documents_link_onclick_dialog.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Custom Dialog for custom link options -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/dlol_imageOpen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        />

    <Button
        android:id="@+id/dlol_buttonOpen"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/dlol_imageOpen"
        android:text=" Open "
        android:textColor="#FFF"
         />

    <ImageView
        android:id="@+id/dlol_imageShare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="6dp"
        android:layout_below="@+id/dlol_imageOpen"
        />

    <Button
        android:id="@+id/dlol_buttonShare"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/dlol_imageShare"
        android:layout_below="@+id/dlol_buttonOpen"
        android:text=" Edit "
        android:textColor="#FFF"
        />

    <ImageView
        android:id="@+id/dlol_imageEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="6dp"
        android:layout_below="@+id/dlol_imageShare"
        />

    <Button
        android:id="@+id/dlol_buttonEdit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/dlol_imageEdit"
        android:layout_below="@+id/dlol_buttonShare"
        android:text=" Edit "
        android:textColor="#FFF"
        />

    <Button
        android:id="@+id/dlol_dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dlol_buttonEdit"
        android:layout_marginRight="2dp"
        android:layout_marginTop="6dp"
        android:layout_centerHorizontal="true"
        android:text=" Ok " />

</RelativeLayout>

documents_link_editlink.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".documents.UrlActivity" >

    <EditText
        android:id="@+id/documentsLinkEditTitleInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/documentsLinkEditTitleLabel"
        android:ems="10"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/documentsLinkEditTitleLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="@string/documents_link_edit_title" />

    <EditText
        android:id="@+id/documentsLinkEditUrlInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/documentsLinkEditUrlLabel"
        android:ems="10"
        android:inputType="textUri" />

    <Spinner
        android:id="@+id/documentsLinkEditTypeSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/documentsLinkEditTypeLabel" />

    <Spinner
        android:id="@+id/documentsLinkEditCategorySpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/documentsLinkEditCategoryLabel" />

    <Spinner
        android:id="@+id/documentsLinkEditSortAfterSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/documentsLinkEditSortAfterLabel" />

    <TextView
        android:id="@+id/documentsLinkEditCategoryLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/documentsLinkEditTypeLabel"
        android:layout_below="@+id/documentsLinkEditTypeSpinner"
        android:text="@string/documents_link_edit_category" />

    <TextView
        android:id="@+id/documentsLinkEditSortAfterLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/documentsLinkEditCategoryLabel"
        android:layout_below="@+id/documentsLinkEditCategorySpinner"
        android:text="@string/documents_link_edit_sortafter" />

    <TextView
        android:id="@+id/documentsLinkEditTypeLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/documentsLinkEditUrlLabel"
        android:layout_below="@+id/documentsLinkEditUrlInput"
        android:text="@string/documents_link_edit_type" />

    <TextView
        android:id="@+id/documentsLinkEditUrlLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/documentsLinkEditTitleLabel"
        android:layout_below="@+id/documentsLinkEditTitleInput"
        android:text="@string/documents_link_edit_url" />

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/documentsLinkEditSortAfterSpinner" >

        <Button
            android:id="@+id/documents_link_edit_save"
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:layout_weight="0.5" 
            android:scaleType="fitXY"
            android:background="@drawable/button_save"
            />



        <Button
            android:id="@+id/documents_link_edit_cancel"
            android:layout_width="0dp"
            android:layout_height="35dp"
            android:layout_weight="0.5"
            android:scaleType="fitXY"
            android:background="@drawable/button_cancel"
             />
    </TableRow>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

只需从对话框onClick()中启动新活动,并让新活动显示新对话框本身(隐式显示,或者在Intent中传递一些EXTRA,以便它知道要显示的对话框。< / p>