Created
September 17, 2011 02:08
-
-
Save changtimwu/1223543 to your computer and use it in GitHub Desktop.
how to create listener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 1: listener as Activity's internal object. */ | |
| public class ContactsInserter extends Activity { | |
| View.OnClickListener onInsert=new View.OnClickListener() { | |
| public void onClick(View v) { | |
| } | |
| }; | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| Button btn=(Button)findViewById(R.id.insert); | |
| btn.setOnClickListener(onInsert); | |
| } | |
| } | |
| /*2: listener constructed with closure. */ | |
| // main_activity.java | |
| public class main_activity extends FragmentActivity { | |
| /** Called when the activity is first created. */ | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.mainfragment); | |
| } | |
| } | |
| // main_fragment.java | |
| public class main_fragment extends Fragment | |
| { | |
| LinearLayout mLayout; | |
| Button mButton; | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| mLayout = (LinearLayout) inflater.inflate(R.layout.main_view, null); | |
| mButton = (Button) mLayout.findViewById(R.id.btn); | |
| mButton.setOnClickListener(new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| } | |
| }); | |
| return mLayout; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment