Four-dimensional printing (4DP) technologies can expand the functionality of stimuli-responsive devices to enable the integration of multiple stimuli-responsive parts into a compact device. Herein, we used digital light processing three-dimensional printing technique, flexible photocurable resins, and photocurable resins of the temperature-responsive hydrogels comprising N-isopropylacrylamide (NIPAM), N,N'-methylenebis(acrylamide) (MBA), and graphene for 4DP of a lab-on-valve (LOV) solid-phase extraction (SPE) device. This device featured flow manifolds and a monolithic packing connected by four near-infrared (NIR)-actuated temperature-responsive switching valves composed of a poly(NIPAM/MBA) (PNM) ball pushing a flexible membrane. NIR irradiation caused the deswelling of the PNM ball [temperature > volume phase transition temperature (VPTT) of the hydrogel], and the valve was opened to switch the flow direction. The termination of this irradiation caused the swelling of the PNM ball (temperature < VPTT of the hydrogel) to close the valve and thus recover the original flow direction to achieve the automatic NIR-actuated fluid control. The optimized 4D-printed NIR-actuated LOV-SPE device enabled a fully automatic SPE scheme coupled with inductively coupled plasma mass spectrometry for the determination of Mn, Co, Ni, Cu, Zn, Cd, and Pb ions (detection limits = 0.1,6.8 ng L-1). The reliability of this analytical method was validated by determining the metal ions in the four reference materials (CASS-6, SLRS-5, 1643f, and Trace Elements Urine L-2) and environmental water and human urine samples. Our results demonstrated the capability and applicability of 4DP technologies for advancing the automation of LOV-SPE schemes and related analytical methods.