编辑文本字段获得不正确的输出



所以,我有一个测试应用程序,我试图让两个片段通过MainActivity进行通信。经过一些研究,我设法将数据从MainFragment传递到FragmentTwo。但是,每当我从MainFragment传入字符串值时,我都会收到一条消息,说

"android.support.v7.widget.AppCompatEditText{cce883f VFED ..... app:id/editText}"

我找不到这种情况的问题所在。请帮忙。顺便说一句,我知道如何使用意图,并且它们(有时)比片段灵活得多,但这只是为了让我知道我可以在片段上走多远。

这是我的主要活动

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class    MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener,                     MainFragment.OnHeadlineSelectedListener {
private String dataReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

}
@Override
public void sendInformations(String message) {
    FragmentTwo fragmentTwo = new FragmentTwo();
    Bundle args = new Bundle();
    args.putString(fragmentTwo.DATA_RECEIVE, message);
    fragmentTwo.setArguments(args);
    getFragmentManager().beginTransaction()
            .replace(R.id.container, fragmentTwo)
            .commit();
}
@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}
@Override
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);
    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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    FragmentManager fragmentManager = getFragmentManager();
    int id = item.getItemId();
    if (id == R.id.frag_one) {
        fragmentManager.beginTransaction().replace(R.id.container, new FragmentOne()).commit();
    } else if (id == R.id.frag_two) {
        fragmentManager.beginTransaction().replace(R.id.container, new FragmentTwo()).commit();
    } else if (id == R.id.frag_three) {
        fragmentManager.beginTransaction().replace(R.id.container, new FragmentThree()).commit();
    } else if (id == R.id.frag_four) {
        fragmentManager.beginTransaction().replace(R.id.container, new FragmentFour()).commit();
    } else if (id == R.id.main_frag) {
        fragmentManager.beginTransaction().replace(R.id.container, new MainFragment()).commit();

    } else if (id == R.id.nav_share) {
    } else if (id == R.id.nav_send) {
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

主片段类

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class MainFragment extends Fragment   {
OnHeadlineSelectedListener mCallback;
Button sendData;

public interface OnHeadlineSelectedListener {
    public void sendInformations(String message);
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mCallback = (OnHeadlineSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main_fragment, container, false);
    final EditText userInput = (EditText) rootView.findViewById(R.id.editText);
    sendData = (Button) rootView.findViewById(R.id.button);

    sendData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.button) {
                mCallback.sendInformations(userInput.toString());
            }
        }
    });
    return rootView;
}
}

碎片二类

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FragmentTwo extends Fragment {
final static String DATA_RECEIVE = "data_receive";
TextView textView;
String input;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_two, container, false);
    textView = (TextView) rootView.findViewById(R.id.data_receiver2);
    savedInstanceState = getArguments();
    if(savedInstanceState != null) {
        input = savedInstanceState.getString(DATA_RECEIVE);
    }
    textView.setText(input);
    return rootView;
}
}
哦,我

得到的消息是当我运行应用程序,在文本字段中键入并按下按钮时,我收到奇怪的消息。如果有人能告诉我是什么导致了奇怪的输出,我将不胜感激。

如果这是一个错误,我在某些相关的情况下注意到了,那是因为我弃用了 v7 吗?Fragment & v7.片段管理器?

您基本上是将 EditText 作为字符串传递,而不是 EditText 值。

在您的主片段中,将mCallback.sendInformations(userInput.toString());更改为mCallback.sendInformations(userInput.getText().toString());

sendData.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         if (v.getId() == R.id.button) {
             mCallback.sendInformations(userInput.getText().toString());
         }
     }
 });

相关内容

  • 没有找到相关文章

最新更新