Prevent using a button until it is ready
So I have seen some other posts similar, but not quite answering what I am looking for.
Basically onCreate is loading in the content (being textviews and image) and it also opens up another thread to prepare the media player so that when the button is pressed it plays a specific song. If this activity is opened and the button is pressed quickly it does not play anything (which I am assuming because it hasn't had a chance to prepare). If I open the activity and give it a few moments it works just fine. I do realize that it is not best to run the content being loaded in onCreate, but I have tried to get it to load in other threads and it failed miserably. It is all fine with me what I really want to happen is this.
I need some way to prevent use of the button or le开发者_JAVA百科t them know it is loading for about 3-5 seconds. So either make the button be faded out and unusable for 3-5 seconds and then it becomes active or a loading screen that is some what transparent that covers the activity for about 3-5 seconds. If you mention using other threads could you please demonstrate it if it isn't asking to much or show me something like a tutorial (other than googles notes). I don't understand threads very well yet (pretty new to them) and the AsyncThread is pretty confusing to me.
If you need code let me know what you would want posted. Thanks and I appreciate any help.
You can set an setOnPreparedListener
on your MediaPlayer, in this listener you enable your button and be save that the song can be played.
Docs
I would create an xml selector file governing button behaviour. Something like:
<?xml version="1.0" encoding="utf-8"?>
<item android:state_enabled="true" android:drawable="@drawable/lbl_black_matte" />
<item android:state_enabled="false" android:drawable="@android:color/transparent">
</item>
Then, in your activity:
private Button mActivate;
mActivate = (Button) findViewById(R.id.activate); mActivate.setOnClickListener(this);
Then somewhere, you'll check to see if the magic is ready, and activate the button if so:
if (mEnabled) {
mActivate.setEnabled(true);
}
Finally, you'll have to reference the button behaviour selector in the xml document in which the button is created.
android:layout_width="match_parent">
<Button android:id="@+id/activate"
android:background="@drawable/b_behaviour" android:layout_height="wrap_content"
android:layout_margin="8dip" android:layout_width="match_parent"
android:text="@string/setup_label_enable" android:textColor="@android:color/white"
android:textStyle="bold">
</Button>
Please let me know if that's clear or if you need me to explain how to create the click listener.
Good luck!
精彩评论