If (WLOG) we have $X_1=X$ and $X_2=\varnothing$, then we can take $e_1=1$ and $e_2=0$. From now on, assume that $X_1$, $X_2$ are both non-empty.
If a topological space $X$ is a disjoint union $X=X_1\sqcup X_2$, then both $X_1$ and $X_2$ are closed. By the definition of the Zariski topology, $X_1=Z(I)$ and $X_2=Z(J)$ for some ideals $I$ and $J$.
Because $Z(I)$ and $Z(J)$ are not empty, each of $I$ and $J$ has some prime ideal containing it, so that $I,J\neq(1)$. Because every $P\in\mathrm{Spec}(A)$ has $P\in X_1$ or $P\in X_2$ but not both, every $P\in\mathrm{Spec}(A)$ has either $P\supseteq I$ or $P\supseteq J$ but not both. Thus, no $P\in\mathrm{Spec}(A)$ contains $I+J$ (because $I+J\supseteq I$ and $I+J\supseteq J$), so that $I+J$ cannot be a proper ideal, so that $I+J=(1)$.
Thus there are some $x\in I$, $y\in J$ for which $x+y=1$. Because $I,J\neq(1)$, $x$ and $y$ are not units. Because $xy\in I\cap J$ and every $P\in\mathrm{Spec}(A)$ contains one of $I$ and $J$, we have that $xy\in P$ for all $P\in\mathrm{Spec}(A)$ and hence $xy\in\bigcap_{P\in\mathrm{Spec}(A)} P = \mathrm{nil}(A)$, the nilradical. Thus $(xy)^n=0$ for some $n$.
We have $1=(x+y)^n=x^n+xy(\mathsf{stuff})+y^n$, so $1-xy(\mathsf{stuff})=x^n+y^n$, and because $xy$ is nilpotent, $xy(\mathsf{stuff})$ is nilpotent and thus $x^n+y^n$ is a unit, say with inverse $z$. Then $$zx^n=zx^n(1)=zx^n(zx^n+zy^n)=(zx^n)^2+z^2(xy)^n=(zx^n)^2,$$ and similarly $zy^n=(zy^n)^2$. Observe that $$zx^n+zy^n=z(x^n+y^n)=1,\qquad (zx^n)(zy^n)=z^2(xy)^n=0.$$
Thus we have idempotents $e_1=zx^n$, $e_2=zy^n$ such that $e_1+e_2=1$, $e_1e_2=0$, and also such that $$X_1=Z(e_1)=\mathrm{Spec}(e_2 A),\qquad X_2=Z(e_2)=\mathrm{Spec}(e_1 A).$$
(The map $\varphi_1:A\to e_1 A$ defined by multiplication by $e_1$ is a surjective ring homomorphism, and therefore the prime ideals of $e_1 A$ are in bijection with the prime ideals of $A$ containing $\ker(\varphi_1)=(e_2)$, and similarly for $\mathrm{Spec}(e_2A)$.)
(Also note that we have $e_1,e_2\neq 0,1$: because $x$ and $y$ are not units, $zx^n$ and $zy^n$ are not units and hence $zx^n,zy^n\neq1$. If $zx^n=0$, then we would have $x^n=0$ and thus $x^n+y^n=y^n$ would be a unit, so that $y$ would be a unit, contradiction. Thus $zx^n\neq0$, and similarly $zy^n\neq0$.)