2013-07-28 2 views

답변

8

저장하고 문자열을 가져 오지하기 위해 View.setTag()View.getTag()를 사용할 수 있습니다. 따라서 버튼을 눌렀을 때, onClick(View v) 메서드를 사용하여 OnClickListener에 대한 콜백을 할 수 있으므로 v.getTag()을 사용하여 String을 검색 할 수 있습니다. xml 파일 (가정 activity_main.xml)에있는 버튼을 만들어

0

시작

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

     <Button 
      android:id="@+id/btnMessage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="onClick" 
      android:text="Click" /> 

</LinearLayout> 

그런 다음 자신의 활동에서 당신이 그것에서 정보를 검색/그것을 발견하고 추가 할 수 있습니다.

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // find button by id 
     Button btn = (Button)findViewById(R.id.btnMessage); 

     // enclose secret message 
     btn.setTag("Bruce Wayne is Batman"); 
    } 

    // this function is triggered when the button is pressed 
    public void onClick(View view) { 

     // retrieve secret message 
     String message = (String) v.getTag(); 

     // display message in the console 
     Log.d("Tag", message); 
    } 
}  

이 방법은 데이터베이스 키 또는 수퍼 히어로의 비밀 ID와 같은 정보를 숨기는 데 유용합니다.

관련 문제