EditNameDialogListener

EditNameDialogListener

我想将对话框片段“AddFriendDialogFragment.java”中的用户键入的字符串传回调用它的 Activity :“HomeActivity.java”。我通过在“AddFriendDialogFragment.java”:“EditNameDialogListener”中声明的接口进行此操作。但是由于某种原因,HomeActivity没有看到此界面,因此出现了“无法解析符号:“EditNameDialogListener”错误。

“HomeActivity.java”:

package tutorial.com.example.jerryhou.dialogactionbartutorial;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class HomeActivity extends Activity implements EditNameDialogListener
{


@Override
public void onFinishEditDialog(String inputText)
{
    Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
public void showUsernameSearchDialog(View v)
{
    FragmentManager fragmentManager = getFragmentManager();
    DialogFragment newFragment = new AddFriendDialogFragment();
    newFragment.show(fragmentManager, "AddFriendDialog");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
 }

“AddFriendDialogFragment.java”:
package tutorial.com.example.jerryhou.dialogactionbartutorial;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;


public class AddFriendDialogFragment extends DialogFragment
{

    private static final String TAG = "AddFriendDialogFragment";

    public interface EditNameDialogListener
    {
        void onFinishEditDialog(String inputText);
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();


        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View addFriendDialogView = inflater.inflate(R.layout.fragment_add_friend_dialog, null);
        // Set an EditText view to get user input
        final EditText usernameEditText = (EditText) addFriendDialogView.findViewById(R.id.username);

        builder.setView(addFriendDialogView)
                // Add action buttons
                .setPositiveButton(R.string.search, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id)
                    {
                        //Retrieve Username typed in
                        String username_querystr = usernameEditText.getText().toString();

                        //Correctly retrieving query str
                        Log.v(TAG, "Going to search " + username_querystr);

                        //Pass back query str to search in HomeActivity

                        EditNameDialogListener activity = (EditNameDialogListener) getActivity();
                        activity.onFinishEditDialog(usernameEditText.getText().toString());

                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        AddFriendDialogFragment.this.getDialog().cancel();
                    }
                });


        return builder.create();
    }

}

最佳答案

即使它们在同一软件包中,您也不能直接使用EditNameDialogListener,因为它是内部接口

您在这里有2个选择:

->使用EditNameDialogListener完全限定的名称,即AddFriendDialogFragment.EditNameDialogListener

public class HomeActivity extends Activity implements AddFriendDialogFragment.EditNameDialogListener

->显式导入EditNameDialogListener
import tutorial.com.example.jerryhou.dialogactionbartutorial.AddFriendDialogFragment.EditNameDialogListener

public class HomeActivity extends Activity implements EditNameDialogListener

07-26 09:31