【发布时间】:2011-11-27 16:04:16
【问题描述】:
所以基本上无法理解 java 中“this”关键字的用法。已经阅读了大约五个教程,虽然不是很好。有人可以快速解释一下它是如何与以下代码相关的吗?这是一个 android,它将一个 xml 按钮 (btn_confirm) 分配给具有 Button 类型的 b。
Button b = (Button)this.findViewById(R.id.btn_confirm);
b.setonclickListener(this);
完整代码:
public class dic_tut2 extends Activity implements onclickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)this.findViewById(R.id.btn_confirm);
b.setonclickListener(this);
}
public void onclick(View v) {
TextView tv = (TextView)this.findViewById(R.id.tv_welcome);
EditText et = (EditText)this.findViewById(R.id.txt_name);
String text = "Hello, " + et.getText().toString() + ".\n\n";
text += "Welcome to android development. :)";
tv.setText(text);
}
}
【问题讨论】:
-
已经阅读了两遍,无法理解与我发布的两行代码相关的内容,也无法单独理解。感谢您的回复。