馬兆駿的"那年我們十九歲", 真的很貼切的道出我們當時的心境, 那個年少輕狂, 純真真誠的情誼, 永遠是我最為懷念的美好時光, 雖然目前大家都各分東西, 忙於自己的事業與家庭, 很少再相聚, 但我總是會在心裡默默的祝福他們, 願他們都能各有一片天, 平安快樂的生活每一天.
星期日, 7月 14, 2013
星期二, 7月 09, 2013
Android Volume Control
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int current_volume =mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//If you want to player is mute ,then set_volume variable is zero.Otherwise you may supply some value.
int set_volume=0;
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,set_volume, 0);
Android hide a view after a few seconds
Spawn a separate thread that sleeps for 3 seconds then call runOnUiThread to hide the view.
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// Do some stuff
}
});
}
};
Android different ways to handle clicks
http://www.remwebdevelopment.com/dev/a69/Different-Ways-To-Handle-Clicks.html
When I first started learning Android,
I found it a little confusing because there are often many different ways to accomplish a single task.
In my case, I did not have a strong background in Java,
otherwise it probably would have been easier for me to look at code samples an recognize these variations.
The most obvious example of alternative approaches to solving a single problem seems to be the various ways you can handle button clicks.
As far as I know, there are four different ways to add listeners for handling button clicks.
If you know of other ways, please post a comment and share them with us.
Here's a simple application that demonstrates four approaches to handling button clicks.
When you click on any of the buttons, an alert will show up on the screen.
There are two files of interest.
The first is the main xml layout file, which has four buttons in it.
This code should look familiar to you, except for the last button.
A new feature was added in Android 2.1 (API Level 7) which allows you to specify an onClick listener from within the xml layout file.
Here's the xml layout file for our sample application...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Inner Class (btn1)" android:id="@+id/Button01"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:text="Anonymous Inner Class (btn2)"
android:id="@+id/Button02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Implementing an Interface (btn3)"
android:id="@+id/Button03" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Calling From XML Layout (btn4)"
android:id="@+id/Button04" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn4Listener">
</Button>
</LinearLayout>
And here's the main class for our application...
package com.remwebdevelopment.samples.eventhandlers;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
I found it a little confusing because there are often many different ways to accomplish a single task.
In my case, I did not have a strong background in Java,
otherwise it probably would have been easier for me to look at code samples an recognize these variations.
The most obvious example of alternative approaches to solving a single problem seems to be the various ways you can handle button clicks.
As far as I know, there are four different ways to add listeners for handling button clicks.
If you know of other ways, please post a comment and share them with us.
Here's a simple application that demonstrates four approaches to handling button clicks.
When you click on any of the buttons, an alert will show up on the screen.
There are two files of interest.
The first is the main xml layout file, which has four buttons in it.
This code should look familiar to you, except for the last button.
A new feature was added in Android 2.1 (API Level 7) which allows you to specify an onClick listener from within the xml layout file.
Here's the xml layout file for our sample application...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Inner Class (btn1)" android:id="@+id/Button01"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</Button>
<Button android:text="Anonymous Inner Class (btn2)"
android:id="@+id/Button02" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Implementing an Interface (btn3)"
android:id="@+id/Button03" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Calling From XML Layout (btn4)"
android:id="@+id/Button04" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn4Listener">
</Button>
</LinearLayout>
And here's the main class for our application...
package com.remwebdevelopment.samples.eventhandlers;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Main extends Activity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//method 1 - uses an inner class named btn1Listener...
Button btn1 = (Button)findViewById(R.id.Button01);
btn1.setOnClickListener(btn1Listener);
//method 2 - use an anonymous inner class as a listener...
Button btn2 = (Button)findViewById(R.id.Button02);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToastMessage("You clicked btn2 - uses an anonymouse inner class");
}
});
//method 3 - note that this class implements
//the View.OnClickListener interface
//which means that we must implement the onClick()
//method (which you'll find below)..
Button btn3 = (Button)findViewById(R.id.Button03);
btn3.setOnClickListener(this);
//method 4 - look at the method btn4Listener() below
}
//here's the inner class used as a listener for btn1...
private View.OnClickListener btn1Listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");
}
};
//here's a method that you must have when your activity implements the
//View.OnClickListener interface...
@Override
public void onClick(View v) {
showToastMessage("you clicked on a btn3, which uses this Activity as the listener");
}
//here's the handler for btn4 (declared in the xml layout file)...
//note: this method only works with android 2.1 (api level 7), it must be public and
//must take a single parameter which is a View
public void btn4Listener(View v) {
showToastMessage("You clicked btn4 - listener was set up in the XML layout");
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//method 1 - uses an inner class named btn1Listener...
Button btn1 = (Button)findViewById(R.id.Button01);
btn1.setOnClickListener(btn1Listener);
//method 2 - use an anonymous inner class as a listener...
Button btn2 = (Button)findViewById(R.id.Button02);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToastMessage("You clicked btn2 - uses an anonymouse inner class");
}
});
//method 3 - note that this class implements
//the View.OnClickListener interface
//which means that we must implement the onClick()
//method (which you'll find below)..
Button btn3 = (Button)findViewById(R.id.Button03);
btn3.setOnClickListener(this);
//method 4 - look at the method btn4Listener() below
}
//here's the inner class used as a listener for btn1...
private View.OnClickListener btn1Listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
showToastMessage("You clicked btn1 - uses an inner class named btn1Listener");
}
};
//here's a method that you must have when your activity implements the
//View.OnClickListener interface...
@Override
public void onClick(View v) {
showToastMessage("you clicked on a btn3, which uses this Activity as the listener");
}
//here's the handler for btn4 (declared in the xml layout file)...
//note: this method only works with android 2.1 (api level 7), it must be public and
//must take a single parameter which is a View
public void btn4Listener(View v) {
showToastMessage("You clicked btn4 - listener was set up in the XML layout");
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
訂閱:
文章 (Atom)