gpt4 book ai didi

java - 如何使用 Java 启用来自另一个 Activity 的按钮?

转载 作者:行者123 更新时间:2023-11-29 23:20:26 25 4
gpt4 key购买 nike

activity_levels.xml

<Button
android:id="@+id/level2"
android:layout_width="100dp"
android:layout_height="105dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="200dp"
android:layout_marginEnd="130dp"
android:layout_marginRight="198dp"
android:background="#FF0000"
android:text="2"
android:textSize="40sp"
android:enabled="false" />

Levels.java

public class Levels extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_levels);
}
}

activity_level_one_result.xml

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Next Level"
android:textAllCaps="false"
android:onClick="nextLevel"/>

LevelOneResult.java

public class LevelOneResult extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_one_result);
}

public void nextLevel(View view) {
startActivity(new Intent(getApplicationContext(), Levels.class));
// enable the button here
}
}

我想启用 activity_levels.xml 文件中的 2 级按钮。我希望通过在 LevelOneResult.java 中使用 Java 来启用它。正如您在上面看到的,我在要放置代码的位置添加了一个注释部分。

最佳答案

在你的 LevelOneResult.java 中创建一个 interface,如下所示,我在代码的注释中解释:

public class LevelOneResult extends AppCompatActivity {

OnCompleteListener mListener;
//create an listener
public interface OnCompleteListener {
void onComplete(boolean enableOrNot);
}
//attach the listener in the activity
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
this.mListener = (OnCompleteListener)activity;
}
catch (final ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
}

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


}

public void nextLevel(View view) {
startActivity(new Intent(getApplicationContext(), Levels.class));
// enable the button here

here trigger the listener
//true means enable
mListener.onComplete(true);
}
}

在你的Levels.java中,你需要实现接口(interface),从接口(interface)onComplete()获取数据,然后做你的事情

public class Levels extends AppCompatActivity implements LevelOneResult.OnCompleteListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_levels);
}

public void onComplete(boolean enableOrNot ) {
// after the action in LevelOne
// the boolean trigger here..

//here the boolean is true,which u set in levelOneJava
if(enableOrNot){
//then do your stuff here
}
}
}

关于java - 如何使用 Java 启用来自另一个 Activity 的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54419866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com