开发者

Should i Declare a UI Object as "final" to be able to access it?

i have the following situation:

     public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.createcharacter);
            //Referenciando Items do Layout
            Button voltar = (Button) findViewById(R.id.voltar);
            Button criar = (Button) findViewById(R.id.criar);
           final TextView nome = (TextView) findViewById(R.id.nome);
        ...........
     criar.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {

                    if(portraitSelected == false)
                    {
                    Toast.makeText(CreateCharacter.this, "Selecione um Avatar", Toast.LENGTH_SHORT).show();
                    Toast.makeText(CreateCharacter.this, nome.getText(), Toast.LENGTH_SHORT).show();

                    }
                    else if(nome.getText() == null)
                    {
                        Toast.makeText(CreateCharacter.this, "Digite um Nome!", Toast.LENGTH_SHORT).show();
                    }
.......

in order to access

else if(nome.getText() == null)

im needing to declare it FINAL on the beginning of the onCreate() method, is it OK?开发者_如何学编程 if its not, how should i do it?


You need to declare it as final so that you can access it inside of the anonymous View.OnClickListener class. This is a limitation of Java's flavor of closures, but there shouldn't be any major problems with making the reference final.

Short version: it's fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜