Java Hello World例子和添加按钮事件与功能

新建android工程,然后默认“下一步”即可完成创建;

2.添加Button

 

3.在src的MainActivity.java添加以下红色代码


import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

private Button mButton;

@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();
}

mButton=(Button)findViewById(R.id.button1);
mButton.setOnClickListener(buttonOnClickListener);

}

private OnClickListener buttonOnClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "点击按钮事件",
Toast.LENGTH_SHORT).show();
}
};

 

posted @ 2014-10-09 19:19  jenson138  阅读(2023)  评论(0编辑  收藏  举报