2017-10-24 5 views
1

현재 뷰 페이지에서 복사 한 텍스트를 다음 또는 이전 텍스트로 복사 할 때 List.but의 textview에 텍스트를 표시하는 뷰 페이지가 있습니다. textview는 뷰 페이지에서 복사 할 때 다음 또는 이전 텍스트를 복사합니다.

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()){ 
     case R.id.copyid: 

      TextView msg= (TextView) viewPager.findViewById(R.id.textView); 
      String msgs=msg.getText().toString(); 
      Intent sendIntent = new Intent(); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra(Intent.EXTRA_TEXT,msgs); 
      sendIntent.setType("text/plain"); 
      startActivity(Intent.createChooser(sendIntent,"Share Via")); 
      break; 
    } 

및 viewpager 어댑터 클래스

: - - : 여기 내 mainactivity 방법 코드

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layout=inflater.inflate(R.layout.viewpager_layour,null); 
    msg= (TextView) layout.findViewById(R.id.textView); 
    msg.setText(list.get(position)); 
    ((ViewPager) container).addView(layout, 0); 

    return layout; 
} 

답변

0

액세스 할 수있는 경우 listonOptionsItemSelected에 다음

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()){ 
     case R.id.copyid: 

     int i = viewPager.getCurrentItem(); 
     String msgs= i>=0?list.get(i):""; 
     Intent sendIntent = new Intent(); 
     sendIntent.setAction(Intent.ACTION_SEND); 
     sendIntent.putExtra(Intent.EXTRA_TEXT,msgs); 
     sendIntent.setType("text/plain"); 
     startActivity(Intent.createChooser(sendIntent,"Share Via")); 
     break; 


} 

사용할 수 있습니다 그것은 더 빠를 것입니다. 귀하의 코드에서 문제의 중복 ID는 TextView입니다. 살펴보기 this answer.

+0

감사합니다. 완벽한 해결책 – AkashK

+0

@AkashK이 답변으로 도움이되었다고 생각한다면 동의하십시오. –

+0

답변으로 수락했습니다. 감사 – AkashK