有一段时间了。令人沮丧!想不出也想不出解决办法。我是 Java 和 Android 的狂热分子,这可能解释了我的无知。
在下面的代码中,我想将选择的铃声分配给 Textview ringtoneDisplay - 据我所知,主要问题是 Textview 在对话框内 - 无法从主活动中更改。如何从 Main Activity 更改对话框中的 Textview?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// When a configuration button is clicked that shows a dialog
public void confClicked(View v) {
// Define and create a dialog to display
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.show();
// Dialog has a button to change ringtone
Button changeRing = (Button) dialog.findViewById(R.id.changeRing);
final TextView ringtoneDisplay = (TextView) dialog.findViewById(R.id.ringtone_textview);
changeRing.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Bring up ringtone change dialog
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
MainActivity.this.startActivityForResult(intent, 5);
}
});
}
// Fired after ringtone was selected
protected void onActivityResult (final int requestCode, final int resultCode, final Intent intent)
{
if (resultCode == Activity.RESULT_OK && requestCode == 5)
{
Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null)
{
**// --> This below won't work..
// --> declaring ringtoneDisplay outside confClicked creates a runtime error.
// --> How do you go about updating ringtoneDisplay to display the ringtone that was chosen
ringtoneDisplay.settext(RingtoneManager.getRingtone(this, uri).getTitle(this));**
}
}
}
}