如何在(NoteEdit java)中使用方法speakOut from(Detail java) 所以我在menu_search而不是viewDetails(mDateText)之后使用它。 这是详细课程
public class Detail extends Activity implements TextToSpeech.OnInitListener{
private Button prsstospeak;
public String data;
private int result=0;
private TextToSpeech tts;
private TextView passedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.disp);
tts = new TextToSpeech(this, this);
prsstospeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
//It will called before TTS started
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
//check status for TTS is initialized or not
if (status == TextToSpeech.SUCCESS) {
//if TTS initialized than set language
result = tts.setLanguage(Locale.US);
// tts.setPitch(5); // you can set pitch level
// tts.setSpeechRate(2); //you can set speech speed rate
//check language is supported or not
//check language data is available or not
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "Missing data", Toast.LENGTH_LONG).show();
//disable button
prsstospeak.setEnabled(false);
} else {
prsstospeak.setEnabled(true);
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut() {
if(result!=tts.setLanguage(Locale.US))
{
Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
}else
{
//speak given text
tts.speak(data, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
这是NoteEdie:
public class NoteEdit extends Activity {
public static int numTitle = 1;
public static String curDate = "";
public static String curText = "";
private EditText mTitleText;
private EditText mBodyText;
private TextView mDateText;
private Long mRowId;
private Cursor note;
private NotesDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.note_edit);
setTitle(R.string.app_name);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
mDateText = (TextView) findViewById(R.id.notelist_date);
long msTime = System.currentTimeMillis();
Date curDateTime = new Date(msTime);
SimpleDateFormat formatter = new SimpleDateFormat("d'/'M'/'y");
curDate = formatter.format(curDateTime);
mDateText.setText(""+curDate);
mRowId = (savedInstanceState == null) ? null :
(Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
}
populateFields();
}
public static class LineEditText extends EditText{
public LineEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.BLUE);
}
private Rect mRect;
private Paint mPaint;
@Override
protected void onDraw(Canvas canvas) {
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);
for (int i = 0; i < count; i++) {
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();
super.onDraw(canvas);
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
populateFields();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.noteedit_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
return true;
case R.id.menu_delete:
if(note != null){
note.close();
note = null;
}
if(mRowId != null){
mDbHelper.deleteNote(mRowId);
}
finish();
return true;
//speech button
case R.id.menu_search:
viewDetails(mDateText);
finish();
return true;
case R.id.menu_save:
saveState();
finish();
return true;
/* case android.R.id.home:
finish(); */
default:
return super.onOptionsItemSelected(item);
}
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
if(mRowId == null){
long id = mDbHelper.createNote(title, body, curDate);
if(id > 0){
mRowId = id;
}else{
Log.e("saveState","failed to create note");
}
}else{
if(!mDbHelper.updateNote(mRowId, title, body, curDate)){
Log.e("saveState","failed to update note");
}
}
}
private void populateFields() {
if (mRowId != null) {
note = mDbHelper.fetchNote(mRowId);
startManagingCursor(note);
mTitleText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
mBodyText.setText(note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
curText = note.getString(
note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
}
}
public void viewDetails(View view){
String data= mDbHelper.getALLData();
}
}
如何使用(详情java)中的方法speakOut
(NoteEdit java)
所以我在menu_search而不是viewDetails(mDateText)
之后使用它。
答案 0 :(得分:0)
它不是从另一个活动调用活动中的方法的好方法! 你有2个解决方案:
最简单的方法(我认为):如果你有一种方法(或多种方法) 你使用很多和不同的类(如活动或 片段),将它们放在一个类中并将此类导入到您的类中 活动并使用此方法。
调用包含此方法的活动 startActivityForResult和intent并返回你的结果 活动。有关这种方式的更多信息: