如何在 onCreate 方法中调用 Custom Activity
How to call Custom Activity inside onCreate method
我正在开发一个简单的应用程序,它有两个活动,MainActivity 和 SecondActivity 以及一个扩展 Dialog 的透明 CustomActivity。这
MainActivity 有两个按钮(Yes_button
和 No_button
)。
当用户点击Yes_button
时,通过Intent调用SecondActivity,
CustomActivity 会在它前面。
当用户单击 No_button
时,SecondActivity 也将被调用,但 CustomActivity 不会与其一起被调用。
CustomActivity的调用基于if-else语句表达式。 CustomActivity 有一个跳过按钮,单击时 CustomActivity 将关闭,只有这样用户才能访问 SecondActivity。 SecondActivity 只有一个调用 MainActivity 的按钮,循环继续。
问题
当应用程序启动并且用户点击 No_button
时,SecondActivity 将在没有 CustomActivity 的情况下被调用(正如预期的那样!),但一旦用户
单击 Yes_button
,即使单击 No_button
,SecondActivity 也会继续显示在 CustomActivity 旁边。
预期
我希望在每次单击 Yes_button
时与 CustomActivity 一起调用 SecondActivity,并且在单击 No_button
时只调用 SecondActivity。
activity_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>
MainActivity.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;
}
}
activity_second_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);
}
}
activity_custom_activity.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>
CustomActivity.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();
}
});
}
}
checking.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>
在Android中,要将数据从一个activity传递给另一个,你不需要声明一个静态变量(getNumber
在MainActivity中),你应该使用Intent和捆绑在一起。请参阅以下解决方案。
MainActivity.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();
}
}
我正在开发一个简单的应用程序,它有两个活动,MainActivity 和 SecondActivity 以及一个扩展 Dialog 的透明 CustomActivity。这
MainActivity 有两个按钮(Yes_button
和 No_button
)。
当用户点击
Yes_button
时,通过Intent调用SecondActivity, CustomActivity 会在它前面。当用户单击
No_button
时,SecondActivity 也将被调用,但 CustomActivity 不会与其一起被调用。
CustomActivity的调用基于if-else语句表达式。 CustomActivity 有一个跳过按钮,单击时 CustomActivity 将关闭,只有这样用户才能访问 SecondActivity。 SecondActivity 只有一个调用 MainActivity 的按钮,循环继续。
问题
当应用程序启动并且用户点击 No_button
时,SecondActivity 将在没有 CustomActivity 的情况下被调用(正如预期的那样!),但一旦用户
单击 Yes_button
,即使单击 No_button
,SecondActivity 也会继续显示在 CustomActivity 旁边。
预期
我希望在每次单击 Yes_button
时与 CustomActivity 一起调用 SecondActivity,并且在单击 No_button
时只调用 SecondActivity。
activity_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>
MainActivity.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;
}
}
activity_second_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);
}
}
activity_custom_activity.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>
CustomActivity.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();
}
});
}
}
checking.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>
在Android中,要将数据从一个activity传递给另一个,你不需要声明一个静态变量(getNumber
在MainActivity中),你应该使用Intent和捆绑在一起。请参阅以下解决方案。
MainActivity.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();
}
}