setText 在纵向布局中不起作用 (android)

setText not working in portrait layout (android)

我正在开发一款在横向布局中运行良好的应用程序,但现在我正试图让它在纵向布局中也能运行。问题是,setText 不会更新我试图显示的 TextView 中的文本。在横向布局中调用相同的片段并且效果很好。

MainActivity.java

//If landscape layout 
if(mTwoPane) {
    switch(index) {
        case 0:
        case 3:
            Bundle arguments = new Bundle();
            TextFragment tf = new TextFragment();
            if (index == 0) {
                arguments.putString("typ", "info");
            } else {
                arguments.putString("typ", "kontakt");
            }
            tf.setArguments(arguments);
            ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.details, tf);
            ft.commit();
            break;
        case 2:
           ..
           ..
}
//If portrait layout
else{
    Context context = view.getContext();
    switch(index) {
        case 0:
        case 3:
            Intent intent = new Intent(context, TextActivity.class);
            if(index == 0){
                intent.putExtra("typ", "info");
            }else{
                intent.putExtra("typ", "kontakt");
            }
            context.startActivity(intent);
            break;
        default:
            break;
    }
}

TextActivity.java

public class TextActivity extends AppCompatActivity {

    FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.portrait_details_layout);

        if (savedInstanceState == null) {
            Bundle arguments = new Bundle();
            arguments.putString("typ", getIntent().getStringExtra("typ"));

            TextFragment fragment = new TextFragment();
            fragment.setArguments(arguments);

            ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.detailss, fragment);
            ft.commit();
        }
    }
}

TextFragment.java

public class TextFragment extends Fragment {

    private static TextView textview;
    private String typ;
    private static final String TAG = TextFragment.class.getName();

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        if(getArguments().containsKey("typ")){
            typ = getArguments().getString("typ");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.text_fragment, container, false);

        textview = (TextView) view.findViewById(R.id.textView1);
        setText();

        Log.d(TAG, "typ = " + typ +"\ntextview.getText = " + textview.getText().toString());
        return view;
    }

    private void setText(){
        if(typ == "kontakt") {
            textview.setText("Kontakt..");
        }else if(typ == "info"){
            textview.setText("Info..");
        }
    }
}

text_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="?android:textAppearanceLarge"
    android:autoLink="phone|email"/>

portrait_details_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.app.MainActivity"
    android:baselineAligned="false">

    <FrameLayout
        android:id="@+id/detailss"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

在MainActivity.java中,如果mTwoPane = true,我直接创建片段的实例(并将其显示在我拥有的ListView的右侧),但如果它处于纵向模式,我希望它是单独显示,没有 ListView,因此我调用了一个 Activity,它链接到另一个布局 (portrait_details_layout.xml)。

在 TextFragment.java 中,我在 onCreateView 中向 Logcat 打印了一条消息。如果我 运行 应用程序并尝试以横向模式查看片段,消息会打印:

索引== 0:

com.example.app.TextFragment: typ = info
                              textview.getText = Info..

索引== 3:

com.example.app.TextFragment: typ = kontakt
                              textview.getText = Kontakt..

这正是我想要的。现在,如果我在纵向模式下做同样的事情:

索引== 0:

com.example.app.TextFragment: typ = info
                             textview.getText = 

索引== 3:

com.example.app.TextFragment: typ = kontakt
                              textview.getText = 

它只是显示一个空白屏幕。但是,如果我对 text_fragment.xml 中的文本进行硬编码,它会显示该文本,并且 textview.getText 也会 return 该文本。

我在这里错过了什么?

我认为问题在于您在 TextFragment class 中的自定义 setText() 中比较 String 的方式。您应该使用 .equals() 方法而不是 == 运算符。

例如here or here您可以阅读更多相关信息。