新聞中心

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

HelloWorld及Android項目結構介紹

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

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 為按鈕添加點擊事件:

要為按鈕添加點擊事件,我們需要先得到這個對象,然后設置監(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中設置的界面控件對象的,這個id是在button中指定的android:id=@+id/Button01 。

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


上一頁 1 2 下一頁

關鍵詞:

評論


相關推薦

技術專區(qū)

關閉