从当前活动访问不同的布局参数

时间:2012-11-09 14:30:15

标签: android android-layout

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

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

有没有办法可以tv设置Main.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

3 个答案:

答案 0 :(得分:3)

您可以随时为所需的任何XML布局充气:

    View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, null);

答案 1 :(得分: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"); 

答案 2 :(得分: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");