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 中调试上面的代码时,视图的值在变量窗口中显示为空。问题是什么?或者是否有任何其他方法可以设置 textview 的 ID,并在不使用 XML 文件的情况下从该特定 ID 检索该文本视图。