扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

android濡備綍璋冪敤activity鏂规硶

扬州沐宇科技
2023-08-08 19:26:38
activity, android

瑕佽皟鐢ㄤ竴涓狝ctivity鐨勬柟娉曪紝浣犻渶瑕佸厛鑾峰彇璇ctivity鐨勫疄渚嬶紝鐒跺悗浣跨敤璇ュ疄渚嬫潵璋冪敤鏂规硶銆備互涓嬫槸鍑犵甯哥敤鐨勬柟娉曪細

  1. 浣跨敤Intent鍚姩Activity骞朵紶閫掑弬鏁帮細
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("key", value);
context.startActivity(intent);

鍦╕ourActivity涓紝鍙互閫氳繃getIntent鏂规硶鑾峰彇浼犻€掔殑鍙傛暟锛?/p>

String value = getIntent().getStringExtra("key");
  1. 浣跨敤鎺ュ彛鍥炶皟锛?/li>

鍦ㄤ綘鐨凙ctivity涓畾涔変竴涓帴鍙o細

public interface MyInterface {
void onMethodCalled();
}

鐒跺悗鍦ˋctivity涓疄鐜拌鎺ュ彛锛屽苟鍦ㄩ渶瑕佽皟鐢ㄧ殑鍦版柟璋冪敤璇ユ帴鍙g殑鏂规硶锛?/p>

public class YourActivity extends AppCompatActivity implements MyInterface {
@Override
public void onMethodCalled() {
// 鍦ㄨ繖閲屽疄鐜颁綘鐨勬柟娉曢€昏緫
}
}

鍦ㄥ彟涓€涓被涓紝灏哬ourActivity鐨勫疄渚嬩紶閫掔粰浣犺璋冪敤鏂规硶鐨勭被锛屽苟璋冪敤鎺ュ彛鏂规硶锛?/p>

public class OtherClass {
private MyInterface myInterface;
public OtherClass(MyInterface myInterface) {
this.myInterface = myInterface;
}
public void callMethod() {
myInterface.onMethodCalled();
}
}

鍦ㄤ綘鐨凙ctivity涓垱寤篛therClass鐨勫疄渚嬶紝骞朵紶閫扽ourActivity鐨勫疄渚嬶細

OtherClass otherClass = new OtherClass(this);

鐒跺悗浣犲彲浠ヨ皟鐢∣therClass鐨勬柟娉曪紝浠庤€岄棿鎺ヨ皟鐢╕ourActivity涓殑鏂规硶銆?/p>

  1. 浣跨敤闈欐€佹柟娉曪細

鍦ㄤ綘鐨凙ctivity涓畾涔変竴涓潤鎬佹柟娉曪細

public static void yourMethod() {
// 瀹炵幇鏂规硶閫昏緫
}

鍦ㄥ叾浠栫被涓紝鐩存帴璋冪敤璇ラ潤鎬佹柟娉曪細

YourActivity.yourMethod();

娉ㄦ剰锛氶潤鎬佹柟娉曞彧鑳借皟鐢ㄩ潤鎬佸彉閲忓拰闈欐€佹柟娉曪紝鏃犳硶鐩存帴璁块棶闈為潤鎬佹垚鍛樺彉閲忓拰鏂规硶銆?/p>

扫码添加客服微信