新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > HelloWorld及Android項(xiàng)目結(jié)構(gòu)介紹

HelloWorld及Android項(xiàng)目結(jié)構(gòu)介紹

作者: 時(shí)間:2016-10-08 來源:網(wǎng)絡(luò) 收藏

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, 運(yùn)行一下(右鍵點(diǎn)擊你的項(xiàng)目,run as -> Android Application)看到一個(gè)按鈕了吧

6 為按鈕添加點(diǎn)擊事件:

要為按鈕添加點(diǎn)擊事件,我們需要先得到這個(gè)對象,然后設(shè)置監(jiān)聽器,再編寫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中設(shè)置的界面控件對象的,這個(gè)id是在button中指定的android:id=@+id/Button01 。

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


上一頁 1 2 下一頁

關(guān)鍵詞:

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉