先上代码:TextView labelView;
CheckBox checkBox;
labelView = (TextView)findViewById(R.id.label);
checkBox = (CheckBox)findViewById(R.id.block);
if(checkBox==null)System.out.println("checkBox为空!");
if(labelView==null)System.out.println("labelView空!");
如上一个TextView和一个CheckView的使用,发现checkview那句有打印……为什么check一直是空的?
CheckBox checkBox;
labelView = (TextView)findViewById(R.id.label);
checkBox = (CheckBox)findViewById(R.id.block);
if(checkBox==null)System.out.println("checkBox为空!");
if(labelView==null)System.out.println("labelView空!");
如上一个TextView和一个CheckView的使用,发现checkview那句有打印……为什么check一直是空的?
解决方案 »
- [个人作品,含源码] Android 程序之在线词典, 觉得有帮助的,请过来顶一下
- 谁有商城的代码(该论坛楼主这样的,如果谁收到他的代码麻烦给我下,急用)
- android 2.1来电
- 用simplecursorAdapter实现播放列表当前正在播放
- XML编辑器
- android 文本框的属性怎样在代码中动态实现
- android 如何创建一个圆形的窗口,像气泡那样,而且可以随机浮动?请大神赐教~!
- 能不能恢复被覆盖的java文件
- 开发android应用的总体思路是什么样的?
- 这是我见过的最奇葩的事,明明调用带五个参数的Matrix.translateM();可调试时却进到了带七个参数的Matrix.translateM(),有图有码
- android EditView 和 TextView 内容中插入图片
- 求助关于adb的输出流问题。
是只打印出checkbox为空那句话,其他的没有了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox
android="@+id/block"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回true,阻止将按键时间传递给界面元素"/>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按键事件信息"/>
</LinearLayout>
这是layout代码!
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;public class KeyEventActivity extends Activity { EditText entryText;
TextView labelView;
CheckBox checkBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
entryText = (EditText)findViewById(R.id.entry);
labelView = (TextView)findViewById(R.id.label);
checkBox = (CheckBox)findViewById(R.id.block);
if(checkBox==null)System.out.println("checkBox为空!");
if(labelView==null)System.out.println("labelView为空!");
entryText.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent keyEvent)
{
int metaState = keyEvent.getMetaState();
int unicodeChar = keyEvent.getUnicodeChar();
String msg="";
msg += "按键动作:"+String.valueOf(keyEvent.getAction())+"\n";
msg += "按键代码:"+String.valueOf(keyCode)+"\n";
msg += "按键字符:"+(char)unicodeChar+"\n";
msg += "UNICODE:"+String.valueOf(unicodeChar)+"\n";
msg += "重复次数:"+String.valueOf(keyEvent.getRepeatCount())+"\n";
msg += "功能键状态:"+String.valueOf(metaState)+"\n";
msg += "硬件编码:"+String.valueOf(keyEvent.getScanCode())+"\n";
msg += "按键标志:"+String.valueOf(keyEvent.getFlags())+"\n";
labelView.setText(msg);
/*if(checkBox.isChecked())
return true;
else*/
// TODO Auto-generated method stub
return false;
}
});
}
}
完整代码!
换ID是什么意思?我把ID名改成block1运行之后还是一样的诡异……为什么只有checkbox没能赋值上,其他的像TextView和EditText都能赋值呢~我查了checkbox的SDK,使用方法也是通过这个findviewbyid()的方法来赋值的。现在搞不懂难道是我装的软件版本问题?
有没有人用我代码在自己机上试试?看看是不是只有我出现了这个问题!
android="@+id/block"
->
<CheckBox
android:id="@+id/block"
不过这eclipse平时对错误那么敏感~这个时候却偷懒……