During Android development, you can always meet the Context class or its instances. Context instances are often used to provide reference for "Applications. For example, in the following code snippet, the first parameter of the Toast class accepts a Context object:
[Java]
@ Override
Protected Dialog onCreateDialog (int id ){
Switch (id ){
Case 0:
Builder builder = new AlertDialog. Builder (this );
Builder. setIcon (R. drawable. ic_launcher );
Builder. setTitle ("This is a dialog with some simple text ...");
Builder. setPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "OK clicked! ",
Toast. LENGTH_SHORT). show ();
}
});
Builder. setNegativeButton ("Cancel ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "Cancel clicked! ",
Toast. LENGTH_SHORT). show ();
}
});
Builder. setMultiChoiceItems (items, itemsChecked,
New DialogInterface. OnMultiChoiceClickListener (){
Public void onClick (DialogInterface dialog, int which,
Boolean isChecked ){
Toast. makeText (
GetBaseContext (),
Items [which]
+ (IsChecked? "Checked! "
: "Unchecked! "),
Toast. LENGTH_SHORT). show ();
}
});
Return builder. create ();
}
Return null;
}
@ Override
Protected Dialog onCreateDialog (int id ){
Switch (id ){
Case 0:
Builder builder = new AlertDialog. Builder (this );
Builder. setIcon (R. drawable. ic_launcher );
Builder. setTitle ("This is a dialog with some simple text ...");
Builder. setPositiveButton ("OK ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "OK clicked! ",
Toast. LENGTH_SHORT). show ();
}
});
Builder. setNegativeButton ("Cancel ",
New DialogInterface. OnClickListener (){
Public void onClick (DialogInterface dialog,
Int whichButton ){
Toast. makeText (getBaseContext (), "Cancel clicked! ",
Toast. LENGTH_SHORT). show ();
}
});
Builder. setMultiChoiceItems (items, itemsChecked,
New DialogInterface. OnMultiChoiceClickListener (){
Public void onClick (DialogInterface dialog, int which,
Boolean isChecked ){
Toast. makeText (
GetBaseContext (),
Items [which]
+ (IsChecked? "Checked! "
: "Unchecked! "),
Toast. LENGTH_SHORT). show ();
}
});
Return builder. create ();
}
Return null;
}
However, the Toast class is not directly used in the Activity and is used in the AlertDialog class. Therefore, you must use the getBaseContext () method to obtain an instance of the Context class.
When a view is dynamically created in an Activity, Context is also displayed. For example, if you want to dynamically create a TextView with excellent encoding:
[Java]
TextView TV = new TextView (this );
TextView TV = new TextView (this); the constructor of TextView accepts a Context object. Because the Activity class is a subclass of the Context class, you can use the this keyword to replace the Conext object.
Tip:
Using this to dynamically create views, such as TextView and Button, poses a potential risk-Memory leakage. Therefore, replace this with the getApplicationContext () method whenever possible.
From horsttnann