我正在尝试学习Android。我想构建一个简单的字母计数应用程序来计算随机名称,这是我的代码我有onclickListerner的问题。提前感谢您Counting Themed Game
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
TextView nameView = (TextView)findViewById(R.id.nameView);
nameView.setText("saya");
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
TextView nameView = (TextView)findViewById(R.id.nameView);
nameView.setText("van");
}
return super.onOptionsItemSelected(item);
}
}
});
}
答案 0 :(得分:0)
替换它:
b.setOnClickListener() ;
有了这个:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Your code here
}
});
删除它:
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stu
}
答案 1 :(得分:0)
由于您要扩展onclicklistener,因此应调用b.setOnClickListener(this);