Android 简单的按钮事件
2011-02-25 13:30
302 查看
今天编写了一个小程序,运行的时候总是出现如下的错误
找了好久,问了eoeAndroid群里的高手们才知道,我把setContentView(R.layout.main)放到了,后面,应该将其放到onCreate最前面,如下
package org.loulijun.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class HelloActivity extends Activity{
/** Called when the activity is first created. */
private Button blue;
private Button yellow;
LinearLayout linearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout=(LinearLayout)this.findViewById(R.id.linearLayout);
blue=(Button)findViewById(R.id.blueButton);
yellow=(Button)findViewById(R.id.yellowButton);
blue.setOnClickListener(new ButtonListener());
yellow.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.blueButton:
linearLayout.setBackgroundColor(Color.BLUE);
break;
case R.id.yellowButton:
linearLayout.setBackgroundColor(Color.YELLOW);
break;
}
}
}
}
----------------------------------------
另外就是要为LinearLayout设置一个Id,然后通过ID来找到这个布局文件,后面的事件里才能够通过
linearLayout.setBackgroundColor(Color.BLUE);
设置背景颜色
main.xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout"
>
<Button
android:id="@+id/blueButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/blue"
/>
<Button
android:id="@+id/yellowButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/yellow"
/>
</LinearLayout>
运行结果是
找了好久,问了eoeAndroid群里的高手们才知道,我把setContentView(R.layout.main)放到了,后面,应该将其放到onCreate最前面,如下
package org.loulijun.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class HelloActivity extends Activity{
/** Called when the activity is first created. */
private Button blue;
private Button yellow;
LinearLayout linearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout=(LinearLayout)this.findViewById(R.id.linearLayout);
blue=(Button)findViewById(R.id.blueButton);
yellow=(Button)findViewById(R.id.yellowButton);
blue.setOnClickListener(new ButtonListener());
yellow.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.blueButton:
linearLayout.setBackgroundColor(Color.BLUE);
break;
case R.id.yellowButton:
linearLayout.setBackgroundColor(Color.YELLOW);
break;
}
}
}
}
----------------------------------------
另外就是要为LinearLayout设置一个Id,然后通过ID来找到这个布局文件,后面的事件里才能够通过
linearLayout.setBackgroundColor(Color.BLUE);
设置背景颜色
main.xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout"
>
<Button
android:id="@+id/blueButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/blue"
/>
<Button
android:id="@+id/yellowButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/yellow"
/>
</LinearLayout>
运行结果是
相关文章推荐
- Android简单的实现音乐播放器功能(一个按钮可相应两个事件的方法)
- android自定义简单Adapter-实现每列按钮事件
- Android基础之响应一个按钮事件的简单Demo
- Android 4.0以上简单实现倒计时按钮
- Android开发学习笔记--给一个按钮定义事件
- Android事件总线分发库EventBus3.0的简单讲解与实践
- Android之简单改变按钮颜色方案
- android 按钮点击事件的四种方法
- android为ListView每个Item上面的按钮添加事件
- [Android开发]android-按钮事件
- Android的按钮单击事件及监听器的实现方式
- Android的按钮单击事件及监听器的实现方式
- Android中Touch事件分析--解决HorizontalScrollView滑动和按钮事件触发问题
- Android事件分发简单理解
- 一个android事件分发的简单Demo
- Android中按钮的点击事件的四种写法
- android 按钮 onClick 五种事件写法
- 响应Android按钮点击事件并且弹出多个对话框的解决办法
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- 在Unity中捕捉Android的常用按钮返回事件