【Android快速入門2】撥號器的實現

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

撥號器是個很簡單的布局,用來做Android的入門最好不過。

本程式給予Android API19編譯實現,因此是新版布局。不習慣的請注意。

下次我有空補上。

1、Java主程式:

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if (savedInstanceState == null) {            getSupportFragmentManager().beginTransaction()                    .add(R.id.container, new PlaceholderFragment())                    .commit();        }    }    public void call(View v){        System.out.println("撥打到電話。");        //讀取號碼        EditText editText=(EditText) findViewById(R.id.eal);        String string=editText.getText().toString();        //撥號        Intent intent=new Intent();        intent.setAction(Intent.ACTION_CALL);        intent.setData(Uri.parse("tel:"+string));        startActivity(intent);    }    @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.     */    public static class PlaceholderFragment extends Fragment {        public PlaceholderFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container, false);            return rootView;        }            }}

2、主清單

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.caller"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="9"        android:targetSdkVersion="19" />    <uses-permission         android:name="android.permission.CALL_PHONE"        />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.caller.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

3、布局清單主Activity

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.caller.MainActivity"    tools:ignore="MergeRootFrame" />

4、布局檔案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"    android:layout_height="match_parent"><TextView     android:id="@+id/cal"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/cal"    /><EditText     android:id="@+id/eal"    android:inputType="number"    android:layout_below="@+id/cal"    android:layout_width="match_parent"    android:layout_height="wrap_content"    /><Button     android:id="@+id/bal"    android:onClick="call"    android:layout_below="@+id/eal"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/bal"    /></RelativeLayout>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.