如何在oncreate方法中调用自定义活动

9avjhtql  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(356)

我正在开发一个简单的应用程序,它有两个活动,mainactivity和secondactivity,还有一个扩展对话框的透明customactivity。main活动有两个按钮( Yes_button 以及 No_button ).
当用户单击时 Yes_button ,第二个活动通过intent调用,customactivity将位于它前面。
当用户单击时 No_button 第二个活动也将被调用,但customactivity不会与之一起被调用。
customactivity的调用基于if else语句表达式。customactivity有一个skip按钮,单击时customactivity将关闭,只有这样用户才能访问secondactivity。secondactivity只有一个按钮调用mainactivity,循环继续。
问题
当应用程序启动,用户点击 No_button ,将调用secondactivity而不调用customactivity(如预期!),但是一旦用户点击 Yes_button ,即使 No_button 已单击。
期望
我希望每次 Yes_button 单击,并且在 No_button 是单击。
活动\u main.xml

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

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1" />

</RelativeLayout>

主活动.java

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static int getNumber;
    Button Yes_button;
    Button No_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Yes_button = findViewById(R.id.button1);
        No_button = findViewById(R.id.button2);
        Yes_button.setOnClickListener(this);
        No_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                int get_input = 1;  // will be use in if else statement.
                getNumber = get_input;
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                break;
            case R.id.button2:
                Intent intent2 = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent2);
                break;

        }
    }

    public static int get_Logic() {
        return getNumber;
    }
}

活动\u second \u activity.xml

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

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:onClick="Click"
        android:text="Click" />

</RelativeLayout>

secondactivity.java文件

public class SecondActivity extends AppCompatActivity {

    private static int output;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_activity);
        int received = MainActivity.get_Logic();
        output = received;
        Display();
    }

    public final void Display() {
        if (output == 1) {
            CustomActivity custom = new CustomActivity(SecondActivity.this);
            custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            custom.show();
        } else {
            CustomActivity custom = new CustomActivity(SecondActivity.this);
            custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            custom.cancel();
        }

    }

    public void Click(View view) {
        Intent intent = new Intent(SecondActivity.this, MainActivity.class);
        startActivity(intent);
    }
}

活动\自定义\活动.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="180dp"
    android:layout_height="250dp"
    xml:toos="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:text="Skip" />

</RelativeLayout>

自定义活动.java

class CustomActivity extends Dialog {

    Button SkipButton;
    private Activity main;

    public CustomActivity(Activity constr) {
        super(constr);
        this.main = constr;
    }

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setCancelable(false);
        setContentView(R.layout.activity_custom_activity);
        SkipButton = findViewById(R.id.button);
        SkipButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}

检查.xml

<?XML version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#FFFF" />
            <corners android:radius="20dp" />
            <stroke android:width="4dp" android:color="@color/colorPrimary" />
            <gradient />
        </shape>
    </item>
</selector>
ctehm74n

ctehm74n1#

在android中,要将数据从一个活动传递到另一个活动,不需要声明静态变量( getNumber 在mainactivity中,应该使用intent和bundle。请参阅下面的解决方案。
主活动.java

public class MainActivity extends BaseActivity implements View.OnClickListener {

    Button Yes_button;
    Button No_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Yes_button = findViewById(R.id.button1);
        No_button = findViewById(R.id.button2);
        Yes_button.setOnClickListener(this);
        No_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int input = v.getId() == R.id.button1 ? 1 : 0;
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("input", input);
        startActivity(intent);
    }
}

secondactivity.java文件

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_activity);
        Display();
    }

    public final void Display() {
        int input = getIntent().getIntExtra("input", 0);
        CustomActivity custom = new CustomActivity(SecondActivity.this);
        custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        if (input == 1) {
            custom.show();
        } else {
            custom.cancel();
        }
    }

    public void Click(View view) {
        finish();
    }
}

相关问题