2012-10-20 65 views
0

如何動態重命名android文本視圖?我想根據編輯文本答案的名稱必須與已經創建的文本視圖相關來分配一個名稱。 例如,名稱a已在xml中分配了一個res id 示例 final TextView name a = (TextView)this.findViewById(R.id.Texti5);重命名android textview

editext.setOnKeyListener(new View.OnKeyListener() { 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     // TODO Auto-generated method stub 
     if (keyCode==KeyEvent.KEYCODE_ENTER) { 
      if("test1".equalsIgnoreCase(anstext.getText().toString())) { 
       // Here i want to give textview1 the 'name a' 
       }} 
      else 
      if("test2".equalsIgnoreCase(editext.getText().toString())) { 
       // Here i want to give textview1 the 'name b' 
      } 

     if("test5".equalsIgnoreCase(editext.getText().toString())) { 
      // Here i want to give textview1 the 'name c' 
     } 

     if("test7".equalsIgnoreCase(editext.getText().toString())) { 
      // Here i want to give textview1 the 'name d' 
     } 
     if (editext.getText().toString() != null){ 
      testwrong.seText("wrong");    } 
     } 
     // here i want to see what name is given and give the name to textview1 so i can 
     perform click on that textview as it is clickable 
     textview1.performClck(); 
    return true; 




    } }); 

回答

1

也許一個可能實現類似目標的方法是根據if語句給textview一個標籤。

textview1.setTag("a"); 

然後你就可以得到標籤回來就好:

if (textview1.getTag() == "a") { 
// do something 
} 

這將在最起碼,比你提出什麼更傳統。