我正在开发一个在横向布局中可以很好地运行的应用程序,但是现在我正在尝试使其也可以在纵向布局中工作。问题是,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也将返回该文本。
我在这里想念什么?
最佳答案
我认为问题在于您在TextFragment类的自定义String
中比较setText()
的方式。您应该使用.equals()
方法而不是==
运算符。
例如,here或here您可以阅读有关它的更多信息。