【问题标题】:setText not working in portrait layout (android)setText 在纵向布局中不起作用(android)
【发布时间】:2016-10-16 12:55:48
【问题描述】:

我正在开发一个在横向布局中运行良好的应用程序,但现在我正试图让它在纵向布局中也能运行。问题是,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,因此我调用了一个链接到另一个布局(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 也会返回该文本。

我在这里错过了什么?

【问题讨论】:

  • 不知何故这看起来不像是一个最小的例子。

标签: java android android-layout android-fragments settext


【解决方案1】:

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

例如herehere,您可以阅读更多相关信息。

【讨论】:

  • 非常感谢!我已经完全排除了变量“typ”的问题,因为 Logcat 消息显示它被赋予了预期值:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
相关资源
最近更新 更多