[英]How to access the variables of a function of a class from another class in Java
[英]How to access another class function from another class (java)
我是Java的新手,目前正在android studio中对其进行测试。
我正在尝试从TEST2类访问TEST类中的函数showMessage() 。
如果在同一类中调用showMessage()函数没有问题,但是如果我尝试从另一个类访问它,代码就会停止。
我的逻辑或语法可能有问题,但是我不确定是哪一个。 我已经尝试寻找一些解决方案,但是找不到可以帮助我解决问题的答案。
这是我的TEST.java代码
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class TEST extends AppCompatActivity {
public TEST2 ab;
Button button;
private final String text ="Testing test test test testing test";
public void showMessage(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
button = (Button) findViewById(R.id.buttontest);
ab = new TEST2(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showMessage(text);
}
});
}
}
class TEST2 extends View{
public TEST t = new TEST();
public TEST2(Context context) {
super(context);
//there is error if I tried this 2 line code
//onTest();
//t.showMessage("TESTING FROM TEST2 CLASS");
}
public void onTest(){
t.showMessage("TESTING FROM TEST2 CLASS");
}
}
这是我的activity_test.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttontest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
您可以通过传递参数上下文来实现
public void showMessage(String message, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
警报对话框是一个UI元素,如果更改了活动,它将不会从另一个类运行。 如果活动没有改变,请尝试...
public void showMessage(final String message,final Context mContext) {
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
});
}
public void onTest(){
if (getContext() instanceof Test)
((Test)getContext()).showMessage("TESTING FROM TEST2 CLASS");
}
只需传递您的TEST2
类的Context
。
public void showMessage(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
.............
......................
}
TEST2:
public TEST2(Context context) {
super(context);
t.showMessage(context, "TESTING FROM TEST2 CLASS");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.