新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > HelloWorld及Android項目結構介紹

        HelloWorld及Android項目結構介紹

        作者: 時間:2016-10-08 來源:網絡 收藏

        setContentView(R.layout.hello_world);

        }

        public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.hello_world);

        }

        R.layout.hello_world 就指向 hello_world.xml,同理 R.string.click_me就向Click

        me, 運行一下(右鍵點擊你的項目,run as -> Android Application)看到一個按鈕了吧

        6 為按鈕添加點擊事件:

        要為按鈕添加點擊事件,我們需要先得到這個對象,然后設置監聽器,再編寫onClick事件

        完成后代碼如下:

        Java代碼

        import android.app.Activity;

        import android.app.AlertDialog;

        import android.os.Bundle;

        import android.view.View;

        import android.view.View.OnClickListener;

        import android.widget.Button;

        public class HelloWorld extends Activity {

        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

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

        button.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {

        openDialog();

        }

        });

        }

        private void openDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle(Hello);

        builder.setMessage(Hello World n);

        builder.setNegativeButton(OK,null);

        builder.show();

        }

        }

        import android.app.Activity;

        import android.app.AlertDialog;

        import android.os.Bundle;

        import android.view.View;

        import android.view.View.OnClickListener;

        import android.widget.Button;

        public class HelloWorld extends Activity {

        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

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

        button.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) {

        openDialog();

        }

        });

        }

        private void openDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle(Hello);

        builder.setMessage(Hello World n);

        builder.setNegativeButton(OK,null);

        builder.show();

        }

        }

        Button button = (Button)findViewById(R.id.Button01);這句話就是用來獲取layout中設置的界面控件對象的,這個id是在button中指定的android:id=@+id/Button01 。

        Android的UI用起來跟SWT是很像的,以后我會挑一些常用的,有趣的UI控件詳細地介紹一下。今天先寫到這里,每次我都會把相應項目的源代碼貼到最下面。


        上一頁 1 2 下一頁

        關鍵詞:

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 崇文区| 方城县| 汾阳市| 苗栗县| 望都县| 宝丰县| 乌拉特中旗| 纳雍县| 丰台区| 县级市| 成安县| 平潭县| 浦县| 乐至县| 安康市| 台中市| 博客| 保德县| 安吉县| 汕尾市| 三门峡市| 清丰县| 乡宁县| 玛纳斯县| 镇坪县| 宁阳县| 明溪县| 朝阳区| 汝南县| 长宁区| 泸水县| 阿尔山市| 都安| 高邮市| 桃园市| 鞍山市| 中方县| 景泰县| 蒙山县| 永仁县| 盖州市|