Android中activity以dialog的形式出现

activity变身为dialog可以实现多功能的dialog,闲话少说直接上代码!

MainActivity.class

/**
 * Activity伪装成的Dialog需要继承Activity
 * 弹窗Activity需要添加主题Theme
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void start(View view) {
        startActivity(new Intent(MainActivity.this, DialogActivity.class));
    }
}

activity_mian.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="jpceshi.jiaxiao.com.dialogactivity.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="跳转" />
LinearLayout>

跳转后出现的dialog样式

DialogActivity .class

/**
在edittext中输入内容后点击button会将内容直接显示到textView中。
*/
public class DialogActivity extends Activity {
    private EditText mEditText;
    private Button mButton;
    private TextView mTextView;
    private String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        initView();
    }

    private void initView() {
        mEditText = (EditText) findViewById(R.id.edtext);
        mTextView = (TextView) findViewById(R.id.tvshow);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str = s.toString();
            }
        });
    }

    public void addText(View view) {
        if (str != null) {
            mTextView.setText(str);
        } else {
            mTextView.setText("null");
        }
    }
}

activity_dialog.xml

<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="200dp"
    android:layout_height="200dp"
    android:orientation="vertical"
    tools:context="jpceshi.jiaxiao.com.dialogactivity.activity.DialogActivity">

    <EditText
        android:id="@+id/edtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:gravity="center"
        android:hint="请输入"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/holo_blue_light" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/button_onoff_indicator_on"
        android:onClick="addText"
        android:text="添加" />

    <TextView
        android:id="@+id/tvshow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@android:color/black" />
LinearLayout>

注:
最重要的一步是:在AndroidManifest.xml文件中给需要显示成dialog样式的
Activity添加 android:theme=”@android:style/Theme.Dialog”(系统自带 的样式,也可以自定义样式)

你可能感兴趣的:(Android)