0

有一段时间了。令人沮丧!想不出也想不出解决办法。我是 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));** 
              }


          }            

      }  


}
4

1 回答 1

0

首先不要使用Dialog,您可以从 Android文档中阅读:

Dialog 类是对话框的基类,但应避免直接实例化 Dialog。相反,使用其中一个子类

这就是我的建议:
创建一个用于显示对话框的方法

private void displayDialog(String ringtone)  

confClicked(View v)调用它。关闭对话框
后, 在通话中再次将所选铃声传递给它。MainActivity.this.startActivityForResult(intent, 5);
OnActivityResultdisplayDialog

于 2013-10-19T12:20:50.287 回答