安卓开发100实例+3个项目源代码下载
http://download.csdn.net/detail/bigtree_mfc/9476150
MainActivity.java文件:星号内部分为添加部分(工程->stc->com.exqmple.q->MainActivity.java)
package com.example.q;
import android.widget.*;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.app.AlertDialog;
import android.app.Dialog;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**************************************************/
Button bt = (Button)findViewById(R.id.Button1);
bt.setOnClickListener(onClick);
}
private View.OnClickListener onClick = new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
new AlertDialog.Builder(MainActivity.this)
.setTitle("对话框")
.setMessage("对话框内容")
.setNegativeButton("确定",null)
.show();
}
};
/*********************************************/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中添加:(工程->res->layout->activity_main.xml)
<Button
android:id="@+id/Button1"
android:layout_width="120dp"
android:layout_height="80dp"
android:layout_below="@id/textView1"
android:text="@string/message"
></Button>
strings.xml中添加:(工程->res->values->strings.xml)
<string name="message">第一个按钮!</string>
界面
运行后,单击按钮:
注:上面的方式实在是太过于麻烦,没有什么卵用,下面介绍一种更简单测试按钮响应的方法:
代码:
设置监听按钮事件:(触发)
{
Toast.makeText(Sample3_18_Activity.this, "123", Toast.LENGTH_SHORT).show();//Toast提示
}