[英]how can I use same XML layout for different activities?
I am new to android development.I want to use same layout file for two different activities.here is my code我是 android 开发的新手。我想对两个不同的活动使用相同的布局文件。这是我的代码
public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton=findViewById(R.id.toggle_button);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
on();
}
}
});
}
public void on()
{
Toast.makeText(this, "button on", Toast.LENGTH_SHORT).show();
}
}
second activity第二项活动
public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
textView.setText("hello MainActivity2");
}
}
});
}
}
My layout file我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<ToggleButton
android:id="@+id/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
here no text is coming on if i checked the button only toast is coming.如果我检查了按钮,这里没有文字出现,只有吐司来了。 I am searching for an answer to this.
我正在寻找这个问题的答案。 I dont know how to use this.
我不知道如何使用这个。 any type of help will be useful.
任何类型的帮助都会有用。
You are providing the constraints for the views which overlap each other.您正在为彼此重叠的视图提供约束。 This way, they do not get visible.
这样,它们就不会被看到。 You must change your layout or constraint the views in different way.
您必须更改布局或以不同方式限制视图。 I will give solution for changing the layout.
我将提供更改布局的解决方案。
Try this code in your xml:在您的 xml 中尝试此代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp" />
<ToggleButton
android:id="@+id/toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout >
Another issue might be occurring.可能正在发生另一个问题。 Let me explain it here.
让我在这里解释一下。
You are only setting the text when the button is checked.您仅在选中按钮时设置文本。 Not when it is not checked.
没有检查时不会。 Try to.
尝试。 display different text every time it's checked or not.
每次检查或不检查时显示不同的文本。 Try this code in second activity:
在第二个活动中尝试此代码:
public class MainActivity2 extends AppCompatActivity {
ToggleButton toggleButton;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text_view);
toggleButton=findViewById(R.id.toggle_button);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
textView.setText("hello MainActivity2");
} else {
textView.setText("bye MainActivity2");
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.