开发者

Java: type casting question

开发者 https://www.devze.com 2023-02-18 15:10 出处:网络
Is there any way to do this in a single line: TextView tv = (TextView) findViewById(R.id.lbSightName);

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

精彩评论

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