天天看点

Android控件

今天给大家实现一个控件功能,具体见图

<a href="http://curran.blog.51cto.com/attachment/201103/3/2788306_1299163448OvNL.png"></a>

<a href="http://curran.blog.51cto.com/attachment/201103/3/2788306_1299163449qo3E.png"></a>

1:设置layout/main.xml布局

&lt;?xml version="1.0" encoding="utf-8"?&gt; 

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

        android:orientation="vertical" 

        android:layout_;fill_parent" 

        android:layout_height="fill_parent" 

        &gt; 

        &lt;EditText 

                android:layout_;fill_parent" 

                android:layout_height="wrap_content" 

                android:id="@+id/edt1" 

        /&gt; 

        &lt;TextView 

                android:text="@string/tv" 

                android:id="@+id/tv" 

                android:id="@+id/edt2" 

        &lt;Button 

                android:id="@+id/button" 

                android:text="@string/button" 

&lt;/LinearLayout&gt;

2:创建计算返回结果的布局layout/result.xml

&lt;LinearLayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_;fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="vertical" 

    &gt; 

            &lt;TextView 

                    android:id="@+id/result" 

                    android:layout_;fill_parent" 

                    android:layout_height="wrap_content" 

            /&gt; 

3:填充布局所对应的values/string.xml

&lt;resources&gt; 

        &lt;string name="hello"&gt;Hello World, ActivityDemo!&lt;/string&gt; 

        &lt;string name="app_name"&gt;ActivityDemo&lt;/string&gt; 

        &lt;string name="button"&gt;计算&lt;/string&gt; 

        &lt;string name="tv"&gt;乘以&lt;/string&gt; 

        &lt;string name="exit"&gt;退出&lt;/string&gt; 

        &lt;string name="about"&gt;关于&lt;/string&gt; 

&lt;/resources&gt;

4:实现布局功能的ActivityDemo.java代码

package com.android.test;

import android.app.Activity; 

import android.content.Intent; 

import android.os.Bundle; 

import android.view.Menu; 

import android.view.MenuItem; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.EditText; 

import android.widget.TextView;

public class ActivityDemo extends Activity { 

        private EditText factorOne, factorTwo; 

        private TextView symbol; 

        private Button calculate; 

        public void onCreate(Bundle savedInstanceState) { 

                super.onCreate(savedInstanceState); 

                setContentView(R.layout.main); 

                //根据控件的ID来取得代表控件的对象 

                factorOne = (EditText)findViewById(R.id.edt1); 

                factorTwo = (EditText)findViewById(R.id.edt2); 

                symbol = (TextView)findViewById(R.id.tv); 

                calculate = (Button)findViewById(R.id.button); 

             //将监听器的对象绑定到按钮对象上面 

                calculate.setOnClickListener(new CalculateListener()); 

        } 

        //当客户点击MENU按钮的时候,调用该方法 

        public boolean onCreateOptionsMenu(Menu menu) { 

                menu.add(0, 1, 1, R.string.exit); 

                menu.add(0, 2, 2, R.string.about); 

                return super.onCreateOptionsMenu(menu); 

        //当客户点击菜单当中的某一个选项时,会调用该方法 

        public boolean onOptionsItemSelected(MenuItem item) { 

                if(item.getItemId() == 1) { 

                        finish(); 

                }else if(item.getItemId() == 2) { 

                } 

                return super.onOptionsItemSelected(item); 

        }

        class CalculateListener implements OnClickListener {

                public void onClick(View v) { 

                        String factorOneStr = factorOne.getText().toString(); 

                        String factorTwoStr = factorTwo.getText().toString(); 

                        //封装起来,将这俩个值放到Intent对象中 

                        Intent intent = new Intent(); 

                        intent.putExtra("one", factorOneStr); 

                        intent.putExtra("two", factorTwoStr); 

                        intent.setClass(ActivityDemo.this, ResultActivity.class); 

                        ActivityDemo.this.startActivity(intent); 

}

5:实现转换为计算结果的布局ResultActivity.java代码

public class ResultActivity extends Activity {

        private TextView resultView; 

        protected void onCreate(Bundle savedInstanceState) { 

                setContentView(R.layout.result); 

                resultView = (TextView)findViewById(R.id.result); 

                Intent intent = getIntent(); 

                String factorOneStr = intent.getStringExtra("one"); 

                String factorTwoStr = intent.getStringExtra("two"); 

                //转换int类型 

                int factorOneInt = Integer.parseInt(factorOneStr); 

                int factorTwoInt = Integer.parseInt(factorTwoStr); 

                int result = factorOneInt * factorTwoInt; 

                resultView.setText(result + ""); 

6:必须在AndroidManifest.xml注册ResultActivity.java

&lt;activity android:name=".ResultActivity"&gt; 

&lt;/activity&gt;

本文转自 Art_Hero 51CTO博客,原文链接:http://blog.51cto.com/curran/506006,如需转载请自行联系原作者

继续阅读