AlertDialogActivity彈框

TTMMJJ99發表於2017-11-30

不一樣的彈框

Style樣式

<style name="DialogTransparent" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowIsTranslucent">true</item>
</style>
String值//後邊listview用的上

<string-array name="lists">
    <item>我的第1個朋友</item>
    <item>我的第2個朋友</item>
    <item>我的第3個朋友</item>
    <item>我的第4個朋友</item>
    <item>我的第5個朋友</item>
    <item>我的第6個朋友</item>
    <item>我的第7個朋友</item>
    <item>我的第8個朋友</item>
    <item>我的第9個朋友</item>
    <item>我的第10個朋友</item>
</string-array>
color背景
<color name="gray">#bcbcbc</color>
//第一個Activity佈局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.bwie.activitydialog.MainActivity">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/img"
    android:src="@mipmap/aa"
    />
</android.support.constraint.ConstraintLayout>

//主Activity主要程式碼
package com.bwie.activitydialog;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView img=findViewById(R.id.img);
       //設定彈框觸發事件
      img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //跳轉到彈框Activy
         Intent intent= new Intent(MainActivity.this,Main2Activity.class);
           //接收選擇的值
         startActivityForResult(intent,0);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String con=data.getStringExtra("date");
        Toast.makeText(MainActivity.this,"獲得值::::"+con,Toast.LENGTH_SHORT).show();
    }
}
AlertdialogActivity佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.bwie.activitydialog.Main2Activity">
    <ListView
        android:layout_centerInParent="true"
        android:id="@+id/list"
        android:background="@color/gray"
        android:layout_width="400dp"
        android:layout_height="600dp"
        android:entries="@array/lists"></ListView>
</RelativeLayout>
設定alertdialogactivity樣式
<activity
    android:name=".Main2Activity"
    android:theme="@style/DialogTransparent"></activity>
alertdialog主要程式碼
package com.bwie.activitydialog;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Main2Activity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ListView listView = findViewById(R.id.list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //獲取值
                String cardNumber = Main2Activity.this.getResources().getStringArray(R.array.lists)[position];
                Intent intent = new Intent();
                intent.putExtra("date", cardNumber);
                // 設定結果,並進行傳送
                setResult(1, intent);
                finish();
            }
        });

    }
}

相關文章