在app中设置一个按钮按下按钮后可以出现一个不覆盖原来界面的小界面里面有另外三个按钮
,分别表示不同的功能。
实现方法如下:
-
在布局文件中,添加一个按钮和一个FrameLayout布局,用于显示小界面。
-
在Activity中,获取按钮和FrameLayout的引用,并设置按钮的点击事件。
-
在按钮的点击事件中,创建一个新的Fragment,并将其加入到FrameLayout中显示。
-
在新的Fragment中,添加三个按钮,并分别设置它们的点击事件。
-
在点击事件中,实现对应的功能,如弹出对话框、跳转新的Activity等。
示例代码:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/show_dialog_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:layout_centerInParent="true"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Button mShowDialogButton;
private FrameLayout mFragmentContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowDialogButton = findViewById(R.id.show_dialog_button);
mFragmentContainer = findViewById(R.id.fragment_container);
mShowDialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFragment();
}
});
}
private void showFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.fragment_container, new MyFragment());
transaction.addToBackStack(null);
transaction.commit();
}
public static class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
Button button1 = view.findViewById(R.id.button1);
Button button2 = view.findViewById(R.id.button2);
Button button3 = view.findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), SecondActivity.class));
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
return view;
}
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Hello World!");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
}
}
fragment_my.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Dialog"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to SecondActivity"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do Something"/>
</LinearLayout>
``
原文地址: https://www.cveoy.top/t/topic/cnq1 著作权归作者所有。请勿转载和采集!