秀气龙

专心、专注、努力

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

android生成屏幕有三种方式:XML配置生成、通过用户界面接口生成、直接用代码生成;

用户界面是View和ViewGroup对象构建而成的,可以进行View和ViewGroup进行随意组合,来完成应用程序界面设计。

下面我介绍一下Button控件的使用方法。

public class ListenerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得mybutton_ok对象
Button mybutton_ok=(Button)findViewById(R.id.mybutton);
//设置按钮控件文本
mybutton_ok.setText("测试按钮");
//建立OnClickListener对象
OnClickListener mylistener = new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
//这里处理事件
DisplayToast("测试");

}
};
//设置mybutton_ok事件监听器
mybutton_ok.setOnClickListener(mylistener);

}
public void DisplayToast(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}

下面是XML文件的Button设置

 <Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>

另外还有一种设置Button控件的事件,就是直接在XML文件中设置onclick事件

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试"
//设置按钮单击事件的函数
android:onClick="test"
/>
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}
public void DisplayToast(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
//设置按钮事件
public void test(View view)
{
DisplayToast("呵呵");
}
}

这里是Button控件的使用方法,具体的看代码一看就明白



 

posted on 2011-11-08 20:38  秀气龙  阅读(156)  评论(0)    收藏  举报