I have a swipe function with some random texts to show up on screen. The texts are Strings in an array within a global context. Im having a problem trying to use a random number generator everytime a swipe occurs AND simultaneously being able to share that random text to applications such as Line or simply as an SMS through text messaging. Within the code below, why doesn't int z update according to the random number generator and update when needed?
int z;
Random randGen=new Random();
public boolean onTouchEvent(MotionEvent touchevent) {
int rando = randGen.nextInt(myNames.length);
z=rando;
final TextView textView = (TextView) findViewById(R.id.textView1);
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/example.ttf");
textView.setTypeface(face);
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]);
}
}
}
return false;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
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(android.content.Intent.EXTRA_TEXT,
"content to Share :" + myNames[z]);
mShare.setShareIntent(shareIntent);
return true;
Aucun commentaire:
Enregistrer un commentaire