
eclipse中jframe如何使用,怎么创建按钮对应的事件,是设置,还是敲代码
2个回答
2015-05-07
展开全部
首先在你的helloworld程序对应的layout配置文件(res/layout/下的XXX.xml文件)中添加一个按钮,具体代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 下面这段就是添加的button -->
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="change background" />
</LinearLayout>
然后在你的继承Activity类的java类中添加按钮的事件监听以及事件处理,代码如下:
public class 你的helloworld类名 extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.你的layout文件名);
//下面的代码用于为按钮注册一个监听
findViewById(R.id.frame_layout).setOnClickListener(new OnClickListener() {
//下面的代码用于处理按钮点击后的事件
public void onClick(View v) {
//下面的代码用于使背景变色
findViewById(R.id.layout).setBackgroundColor(Color.BLUE);
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 下面这段就是添加的button -->
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="change background" />
</LinearLayout>
然后在你的继承Activity类的java类中添加按钮的事件监听以及事件处理,代码如下:
public class 你的helloworld类名 extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.你的layout文件名);
//下面的代码用于为按钮注册一个监听
findViewById(R.id.frame_layout).setOnClickListener(new OnClickListener() {
//下面的代码用于处理按钮点击后的事件
public void onClick(View v) {
//下面的代码用于使背景变色
findViewById(R.id.layout).setBackgroundColor(Color.BLUE);
}
});
}

2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询