Solve the Problem of component acquisition in the fragment_main.xml file of Android

Source: Internet
Author: User

Package com. dhy. phonedial; import android. app. activity; import android. app. fragment; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. button; import android. widget. editT Ext; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); if (savedInstanceState = null) {getFragmentManager (). beginTransaction (). add (R. id. container, new PlaceholderFragment ()). commit () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds Items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {r Eturn true;} return super. onOptionsItemSelected (item);}/*** A placeholder fragment containing a simple view. A placeholder segment contains a simple view. * // *** This internal class is automatically completed by the latest development tool. Its function is to load and parse the fragment_main.xml file and obtain fragment_main. * registered components in xml. ** @ author Martin **/public static class extends Fragment {public PlaceholderFragment () {}@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// obtain the View object according to the final View rootView = inflater in the fragment_main.xml file. inflate (R. layout. fragment_main, container, false); // obtain the component Button dialBtn = (Button) rootView through the view object. findViewById (R. id. btn_dial); System. out. println (rootView. findViewById (R. id. btn_dial) + "==============" + R. id. btn_dial + "==========================" + dialBtn ); // Add events for components // since I am also a newbie, I have not solved the problem of how to obtain components without using anonymous internal classes, dialBtn. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // obtain the component EditText et_number = (EditText) rootView through the View object. findViewById (R. id. edit_number); String number = et_number.getText (). toString (); System. out. println ("====================" + number); Intent intent = new Intent (); intent. setAction (Intent. ACTION_CALL); intent. setData (Uri. parse ("tel:" + number); startActivity (intent) ;}}); return rootView ;}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.