import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class ViewId extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txtArea = new TextView(this);
txtArea.setId(20);
txtArea.setText("Hello");
TextView view = new TextView(this);
view = (TextView) this.findViewById(20);
Toast.makeText(this,view.getText(), Toast.LENGTH_LONG).show();
}
}
當我在eclipse中調試上面的代碼時,視圖的值在變量窗口中顯示爲空。 有什麼問題?或者是否有任何其他方法setid的textview,並從該特定的id回收該文本視圖,而不使用XML文件。findviewbyid在android中不起作用
我認爲你應該花一些時間在http://developer.android.com/resources/tutorials/hello-world.html – ingsaurabh 2011-03-09 11:27:59