0

假设我当前的活动是Main.java并且我已经通过setContentView(R.layout.layout1)它的onCreate方法声明了它的布局。现在,我是否可以通过任何方式访问不同的布局?例如,假设有另一个布局 -layout2具有TextViewid tv,那么我将无法执行以下代码Main.java

TextView text = (TextView) findViewById(R.id.tv);
text.setText("blah blah");

有什么方法可以让我tvMain.java.

我的实际代码如下

setContentView(R.layout.layout);
Button button = (Button) findViewById(button);
    button(buttonListener);
Dialog dialog;

在侦听器内部,我有以下代码:

TextView dialogTitle = (TextView) findViewById(R.id.dialog_title);
        dialogTitle.setText("Email");

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View customView = getLayoutInflater().inflate(R.layout.dialog, null);
        builder.setView(customView);            

        dialog = builder.create();
                    dialog.show();

我面临的问题dialog_title是在 dialog.xml 而不是layout.xml

4

3 回答 3

3

您可以随时扩充您想要的任何 XML 布局:

    View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, null);
于 2012-11-09T14:40:29.013 回答
2

您可以使用Bundles

在活动 1

String your_string = "Hello, World!";
Bundle bundle = new Bundle();
bundle.putString("The key for this string", your_string );

Intent ActivityToLaunch= new Intent(this, ActivityB.class); 
ActivityToLaunch.putExtras(bundle);
this.startActivity(ActivityToLaunch);

在活动 2

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2); //Setup some layout, set to your own

    String content = getIntent().getExtras().getString("The key for this string");
    TextView text = (TextView) findViewById(R.id.tv);
    text.setText(content);     
}

线程启动者说他想提出一个自定义对话框,所以这里是编辑

这是我的类,它将生成一个自定义对话框:

public class ErrorDialog {

    TextView msgTextView;
    Button toSettings;
    final Context c;
    Dialog errorDialog;


   /**
     * @param c The Context
     * @param title Title of the Dialog
     * @param msg Message og the Dialog
     * @param textOnButton The text on the button
     */

    public ErrorDialog(final Context c, String title, String msg, String textOnButton) {

        this.c = c;
        errorDialog = new Dialog(c);
        errorDialog.setContentView(R.layout.error_dialog);
        errorDialog.setTitle(title);

        msgTextView = (TextView) errorDialog.findViewById(R.id.errorMSG);
        msgTextView.setText(msg);

        toSettings = (Button) errorDialog.findViewById(R.id.toSettings);
        toSettings.setText(text);
        toSettings.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

               //doing operations when the user clicks my button in the dialog. 
            } 
        });

        errorDialog.show();
        errorDialog.setCancelable(true);
    }
}

以这种方式使用此类:

new ErrorDialog(getApplicationContext(), "My Title", "My Message to the user", "Text on the button"); 
于 2012-11-09T14:39:35.073 回答
1
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View customView = getLayoutInflater().inflate(R.layout.dialog, null);
        builder.setView(customView);      
TextView dialogTitle = (TextView) customView.findViewById(R.id.dialog_title); 
dialogTitle.setText("Email");
于 2012-11-09T15:02:45.200 回答