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.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } @Override public 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; } @Override public 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) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. 一个占位符片段包括一个简单的视图。 */ /** * 这个内部类是最新的开发工具所自己主动完毕的,其功能就是对fragment_main.xml文件进行载入并解析,能够获取fragment_main. * xml中的已注冊组件. * * @author Martin * */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 获取视图对象依据fragment_main.xml文件 final View rootView = inflater.inflate(R.layout.fragment_main, container, false); // 通过视图对象获取组件 Button dialBtn = (Button) rootView.findViewById(R.id.btn_dial); System.out.println(rootView.findViewById(R.id.btn_dial) + "============" + R.id.btn_dial + "===================" + dialBtn); // 为组件加入事件 // 因为我也是新手还没有解决不使用匿名内部类怎样获取组件的问题, dialBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 相同通过视图对象获取组件 EditText et_number = (EditText) rootView .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; } } }
版权声明:本文博主原创文章,博客,未经同意不得转载。