2014-04-01 1 views
2

문자열을 메시지로 사용하는 toastClickListener를 어떻게 만들 수 있습니까? 여러 언어를 지원하는 앱을 만들고 있는데 토스트를 다른 언어로 보여주고 싶습니다. 그 때문에 Strings를 사용해야합니다. 어떻게 토스트 용 문자열을 가져 와서 보여줄 수 있습니까?메시지에 문자열을 사용하여 toItemClickListener를 토스트합니까?

list.setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, 
      int position, long id) { 
      switch (position) { 
      case 0: 
       showtost(); 
       break; 
       } 
      return true; 

몇 가지 방법을 시도했지만 작동하지 않았습니다.

private void showtost() { 
      // TODO Auto-generated method stub 
      Toast.makeText(this, 
        .getResources().getString(R.string.alarm), 
        Toast.LENGTH_SHORT).show(); 

      return; 
      } 

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new AdapterView.OnItemLongClickListener(){}, 

문자열, int)를

+0

어쩌면 당신의 현재 구현을 추가 할 값 onItemClickListener – donfuxx

+0

U 앱에서 Locale을 변경하고 각 strings.xml에서 현지화 된 문자열을 가져와야합니다. –

+1

실제 질문을 이해할 수 없으며, 다중 언어에 대한 문자열을 정의해야하거나 토스트에 대해 onitemclicklistener가 필요합니까? –

답변

3

this을 컨텍스트 참조로 변경해야합니다. 활동 사용하는 경우 :

MyActivity.this 

또는 조각의 내부에서

:
getActivity() 

그런 다음 당신이 게시 된 예는 작동합니다.

1

예,이 같은 그것을 할 수는 :

여기 hello_world
Toast.makeText(context, R.string.hello_world, Toast.LENGTH_LONG).show(); 

strings.xml의에서 문자열 ID로 대체 될 수있다.

1

거기에 getActivity()를 언급해야합니다.

private void showtost() { 
      // TODO Auto-generated method stub 
      Toast.makeText(getActivity(), 
        .getResources().getString(R.string.alarm), 
        Toast.LENGTH_SHORT).show(); 

      return; 
      } 
1

다국어 지원; 값 폴더에 strings.xml을 정의 할 수 있으며 values-tr과 다른 폴더 이름을 추가 할 수 있습니다 (터키어 용). 다음은이처럼 문자열을 설정할 수 있습니다

String my_toast_string=context.getString(R.string.my_string); 

은 다음과 같이 당신의 축배를 넣을 수 있습니다 :

Toast.makeText(getActivity(),my_toast_string, Toast.LENGTH_SHORT).show(); 
1
당신은 문자열을 얻을 수 있습니다

;

문자열 x = getResources(). getString (R.string.언어);

당신은 언어 설정과 고해상도의 폴더를 만들고 거기에 당신의 문자열 파일을 변경할 수 있습니다 : 당신이 할 경우

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="language">cn</string> 
</resources> 

과 :

폴더 고해상도/값-CN하면 문자열 파일을 폴더 고해상도/값-KO 당신은 문자열을 파일합니다

을 : 지금 당신이 clocklistener에 항목을 설정하고 folowing 코드를 추가하는 경우

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="language">en</string> 
</resources> 

Toast.makeText(context, language, Toast.LENGTH_SHORT).show(); 

출력은

영어 내가 폴더 고해상도의 기본 값을하는 것이 좋습니다 경우 언어 장치가 "EN"을 중국어이며, 경우/"cn이"입니다

관련 문제