自定义activity弹出框

Android有三种方式实现对话框(Dialog,PopupWindow,Activity),这里记录一下Activity的方式。

1. 自定义style

在style.xml文件中自定义一个style

<style name="DialogStyle">
    <!--设置dialog的背景-->
    <item name="android:windowBackground">@android:color/transparent</item>
    <!--设置Dialog的windowFrame框为无-->
    <item name="android:windowFrame">@null</item>
    <!--设置无标题-->
    <item name="android:windowNoTitle">true</item>
    <!--是否浮现在activity之上-->
    <item name="android:windowIsFloating">true</item>
    <!--是否半透明-->
    <item name="android:windowIsTranslucent">true</item>
    <!--设置窗口内容不覆盖-->
    <item name="android:windowContentOverlay">@null</item>
    <!--设置动画,在这里使用让它继承系统的Animation.Dialog-->
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <!--背景是否模糊显示-->
    <item name="android:backgroundDimEnabled">true</item>
</style>

2. 写一个Activity

class

public class InfoActivity extends Activity {
    Button btnPhone;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_info);
    setFinishOnTouchOutside(true);
    
     btnPhone = (Button) findViewById(R.id.btnPhone);
     
     btnPhone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "已拨号", Toast.LENGTH_SHORT).show();
        
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:13866668888"));
        startActivity(intent);
        }
     });
    }
}

值得注意的是如需要点击区域外消失则要下面这段代码

setFinishOnTouchOutside(true);

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="100dp">
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:text="手机:"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <TextView
                    android:text="13866668888"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <Button
                    android:id="@+id/btnPhone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="拨打电话" />
            </LinearLayout>
        </LinearLayout>
</LinearLayout>

3. 注册AndroidManifest

在AndroidManifest注册Activity的时候声明theme是第一步所自定义的style。

<activity android:name=".activity.InfoActivity"
    android:theme="@style/DialogStyle"/>

4. 调用

startActivity(new Intent(activity, InfoActivity.class));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现 Android 自定义框样式,可以按照以下步骤进行: 1. 创建一个自定义布局文件,包含框的视图元素。 2. 创建一个继承自 Dialog 的类,重写其中的 onCreate 方法,在其中设置框的样式和内容。 3. 在 Activity 中创建该自定义框类的对象,并调用 show() 方法实现框的显示。 下面是一个简单的自定义框样式实现的示例代码: 1. 自定义布局文件 dialog_custom.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title" android:textSize="18sp" android:textColor="#000000" android:gravity="center"/> <EditText android:id="@+id/et_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Input something..." android:textColor="#000000"/> <Button android:id="@+id/btn_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="OK" android:textColor="#FFFFFF" android:background="#009688" android:layout_marginTop="16dp"/> </LinearLayout> ``` 2. 自定义框类 CustomDialog.java ```java public class CustomDialog extends Dialog { private TextView tvTitle; private EditText etInput; private Button btnOk; public CustomDialog(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_custom); tvTitle = findViewById(R.id.tv_title); etInput = findViewById(R.id.et_input); btnOk = findViewById(R.id.btn_ok); btnOk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在这里处理按钮点击事件 dismiss(); } }); // 设置框样式 Window window = getWindow(); if (window != null) { window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); WindowManager.LayoutParams layoutParams = window.getAttributes(); layoutParams.gravity = Gravity.CENTER; layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; window.setAttributes(layoutParams); } } // 设置框标题 public void setTitle(String title) { tvTitle.setText(title); } // 获取输入框的内容 public String getInputText() { return etInput.getText().toString(); } } ``` 3. 在 Activity 中使用自定义框 ```java public class MainActivity extends AppCompatActivity { private CustomDialog customDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 创建自定义框对象 customDialog = new CustomDialog(this); // 设置框标题 customDialog.setTitle("Input Something"); // 显示框 customDialog.show(); // 获取输入框的内容 String inputText = customDialog.getInputText(); } } ``` 这样就可以实现一个简单的自定义框样式了。需要注意的是,框的样式可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值