开发者

Java: type casting question

Is there any way to do this in a single line:

    TextView tv = (TextView) findViewById(R.id.lbSightName);
    tv.setText("Some Text");

I would like to do away declaring the intermediary tv, like so:

(TextView) findViewById(R.id.lbSightN开发者_Python百科ame).setText("Some Text");

Not possible?


You can, but it isn't best practice

((TextView) findViewById(R.id.lbSightName)).setText("Some Text");
TextView.class.cast(findViewById(R.id.lbSightName).setText("Some Text");


((TextView) findViewById(R.id.lbSightName)).setText("Some Text");


With one more set of parenthesis it should be possible:

((TextView)findViewById(R.id.lbSightName)).setText("Some Text");


((TextView) findViewById(R.id.lbSightName)).setText("Some Text");

Just add braces.


Sure

((TextView) findViewById(R.id.lbSightName)).setText("Some Text");


((TextView)findViewById(R.id.lbSightName)).setText("Some thingy");

Adding one more set of parenthesis does the trick

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜