我正在尝试在 onCreate Activity 中定义此 onClick 方法
public void GetMusicListLock(View v) {
Intent getNameScreenIntent = new Intent(this, SecondScreen.class);
final int resultLock=1;
getNameScreenIntent.putExtra("Lock","LockScreen");
startActivityForResult(getNameScreenIntent, resultLock);
}
但它给了我错误“无法解析符号 v”。
如果我在 onCreate 之外定义该方法,它就可以正常工作。我想知道是否有办法在 onCreate 中定义 onClick 方法。
最佳答案
在 onCreate 中这样做:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent getNameScreenIntent = new Intent(this, SecondScreen.class);
final int resultLock=1;
getNameScreenIntent.putExtra("Lock","LockScreen");
startActivityForResult(getNameScreenIntent, resultLock);
}
});
关于android - 在onCreate中定义onClick方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771574/