简体   繁体   English

更改按钮上的布局单击Android

[英]Changing Layouts On Button Click In Android

So as of current I am making an Android application with multiple layout files that swap on a button click. 因此,截至目前,我正在制作一个Android应用程序,其中包含多个布局文件,可以按下按钮进行交换 Currently I am having an issue. 目前我遇到了问题。 I have a user click a button that opens an AlertDialog with multiple radio buttons, they select one then hit Ok. 我有一个用户单击一个按钮,打开一个带有多个单选按钮的AlertDialog,然后选择一个然后单击确定。 This sets a global CharSequence Then once they are sure that is what they want they will hit Done to go to the next screen. 这设置了一个全局CharSequence然后一旦他们确定这是他们想要的,他们将点击Done进入下一个屏幕。 Here is the code for the Done button. 这是完成按钮的代码。

<Button android:layout_height="wrap_content" android:id="@+id/doneEventButton"       android:text="@string/doneEventButton" android:layout_width="138dp" android:onClick="nextEvent"></Button>

And here is the code for nextEvent . 这是nextEvent的代码。

public void nextEvent()
{
    if (eventVarString == "Send A Text")
    {
        setContentView(R.layout.send_text);
    }
    else if (eventVarString == "Make A Call")
    {
        setContentView(R.layout.make_call);
    }
    else if (eventVarString == "Open An App")
    {
        setContentView(R.layout.open_app);
    }
    else if (eventVarString == "Send An Email")
    {
        setContentView(R.layout.send_email);
    }
    else if (eventVarString == "Go To A Website")
    {
        setContentView(R.layout.go_to_a_website);
    }
}

Here is the code that pops up the AlertDialog and sets the global variable. 这是弹出AlertDialog并设置全局变量的代码。

public void typeOfEvent(View v)
{
        final CharSequence[] items = {"Send A Text", "Make A Call", "Open An App", "Send An Email", "Go To A Website"};

        AlertDialog.Builder builder = new AlertDialog.Builder(SchedulerActivity.this);
        builder.setTitle("Choose An Event");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                eventVarString = items[item];
         Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });

        builder.setPositiveButton("Yes",
         new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
           TextView addEventVarText = (TextView) findViewById(R.id.eventChosen);
           addEventVarText.setText(eventVarString);
           Toast.makeText(SchedulerActivity.this, "Success", Toast.LENGTH_SHORT).show();
          }
         });
        builder.setNegativeButton("No",
         new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
           Toast.makeText(SchedulerActivity.this, "Fail", Toast.LENGTH_SHORT).show();
          }
         });
        AlertDialog alert = builder.create();
        alert.show();
}

And here is the LogCat that is generated from the Force Close when I click the Done button. 这是我单击“完成”按钮时从“强制关闭”生成的LogCat。

Pastebin.com Pastebin.com

将参数View添加到事件处理程序。

public void nextEvent(View view)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM