我正在尝试编写一个使用滑动功能的应用。每次用户滑动手机时,都会生成一个随机数,该数字用于获取数组myNames []中的字符串值,并显示在textView上。我想使用行或任何文本消息应用程序共享textView上显示的值。我的最终问题是:为什么shareIntent不识别textView.getText()。toString();并启用该字符串值的共享?我一直试图解决这个问题几天,并且无法弄清楚为什么每次刷卡开始时它都不会更新值。
public class MainActivity extends Activity {
float x1, x2;
float y1, y2;
SharedPreferences preferences;
ShareActionProvider provider;
TextView textView;
String z;
private ShareActionProvider mShareActionProvider;
final String myNames[] = {"1","2","3","4","5","6"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
Typeface font = Typeface.createFromAsset(getAssets(),
"fonts/example.ttf");
textView.setTypeface(font);
}
public boolean onTouchEvent(MotionEvent touchevent) {
Random randGen = new Random();
int rando =randGen.nextInt(myNames.length);
switch (touchevent.getAction()) {
// when user first touches the screen we get x and y coordinate
case MotionEvent.ACTION_DOWN: {
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = touchevent.getX();
y2 = touchevent.getY();
if (x1 < x2) {
textView.setText(myNames[rando]);
}
// if right to left sweep event on screen
if (x1 > x2) {
textView.setText(myNames[rando]);
}
}
}z=myNames[rando];
return false;
}
public boolean onCreateOptionsMenu(Menu menu) {
String y="yolo";
getMenuInflater().inflate(R.menu.main, menu);
MenuItem shareItem = (MenuItem) menu.findItem(R.id.action_share);
ShareActionProvider mShare = (ShareActionProvider) shareItem
.getActionProvider();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "text to sahre test: " + textView.getText().toString()
);
mShare.setShareIntent(shareIntent);
return true;
}
}
答案 0 :(得分:0)
onCreateOptionsMenu()
只运行一次 - 最初创建菜单时。
您应该保存对ShareActionProvider
的引用,并在每次setShareIntent()
的文字更改时(例如,作为TextView
的一部分)致电ACTION_UP
。< / p>