android濡備綍璋冪敤activity鏂规硶
瑕佽皟鐢ㄤ竴涓狝ctivity鐨勬柟娉曪紝浣犻渶瑕佸厛鑾峰彇璇ctivity鐨勫疄渚嬶紝鐒跺悗浣跨敤璇ュ疄渚嬫潵璋冪敤鏂规硶銆備互涓嬫槸鍑犵甯哥敤鐨勬柟娉曪細
- 浣跨敤Intent鍚姩Activity骞朵紶閫掑弬鏁帮細
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("key", value);
context.startActivity(intent);
鍦╕ourActivity涓紝鍙互閫氳繃getIntent鏂规硶鑾峰彇浼犻€掔殑鍙傛暟锛?/p>
String value = getIntent().getStringExtra("key");
- 浣跨敤鎺ュ彛鍥炶皟锛?/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>
- 浣跨敤闈欐€佹柟娉曪細
鍦ㄤ綘鐨凙ctivity涓畾涔変竴涓潤鎬佹柟娉曪細
public static void yourMethod() {
// 瀹炵幇鏂规硶閫昏緫
}
鍦ㄥ叾浠栫被涓紝鐩存帴璋冪敤璇ラ潤鎬佹柟娉曪細
YourActivity.yourMethod();
娉ㄦ剰锛氶潤鎬佹柟娉曞彧鑳借皟鐢ㄩ潤鎬佸彉閲忓拰闈欐€佹柟娉曪紝鏃犳硶鐩存帴璁块棶闈為潤鎬佹垚鍛樺彉閲忓拰鏂规硶銆?/p>
相关问答