`
Leif_冬
  • 浏览: 45232 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

android 自定義的對話框(AlertActivity)

 
阅读更多

Step1:

自定義的對話框Activity:

public class EthernetConfigDialog extends AlertActivity implements AdapterView.OnItemSelectedListener, DialogInterface.OnClickListener,
       AlertController.AlertParams.OnPrepareListViewListener {
    private View mView;
    static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
    private RadioButton mDhcpRadioButton,mStaticIpRadioButton;
    private LinearLayout mStaticIpConfigLinearLayout;
    private EditText mIpAddressEditText,mGatewayEditText,mNetworkPrefixLengthEditText,mSubMaskEditText,mDnsFirstEditText,mDnsSecondEditText;
    private Spinner mEthernetDevicesSpinner,mStaticProxy;
    private String tIpAddressString="",tGatewayString="",tNetworkPrefixLengthString="",tSubMaskString,tDnsFirstString="",tDnsSecondString="";
    private String mEthernetDeviceSelect="",mConnectionTypeSelect="",mStaticProxySelect = "";
    private int tEthernetDeviceIndex = 0,tNetworkPrefixLength = 0;
    private EthernetManager mEthManager;
    private IpConfiguration config;
    private static final String PROXY_NONE = "NONE";
    private static final String PROXY_STATIC = "STATIC";
    private static final String PROXY_UNASSIGNED = "UNASSIGNED";
    private static final String PROXY_PAC = "PAC";
    private static final String CONNECT_TYPE_DHCP = "dhcp_connect";
    private static final String CONNECT_TYPE_STATIC = "static_connect";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final AlertController.AlertParams p = mAlertParams;
        p.mIsSingleChoice = true;
        p.mOnItemSelectedListener = this;
        p.mView = getLayoutInflater().inflate(R.layout.ethernet_ap_dialog, null);
        mView = p.mView;
        mDhcpRadioButton = (RadioButton) mView.findViewById(R.id.ethernet_dhcp);
        mStaticIpRadioButton = (RadioButton) mView.findViewById(R.id.ethernet_static_ip);
        mStaticIpConfigLinearLayout = (LinearLayout) mView.findViewById(R.id.static_ip_config);
        mEthernetDevicesSpinner = (Spinner) mView.findViewById(R.id.ethernet_devices);
        mStaticProxy = (Spinner) mView.findViewById(R.id.ethernet_proxy);
        mIpAddressEditText = (EditText) mView.findViewById(R.id.ip_address_input);
        mGatewayEditText = (EditText) mView.findViewById(R.id.gateway_input);
        mSubMaskEditText = (EditText) mView.findViewById(R.id.submask);
        mDnsFirstEditText = (EditText) mView.findViewById(R.id.dns1_input);
        mDnsSecondEditText = (EditText) mView.findViewById(R.id.dns2_input);
        mEthManager = (EthernetManager) getApplication().getSystemService(Context.ETHERNET_SERVICE);
        mStaticProxy.setOnItemSelectedListener(this);
        getEthernetConfigSettings();
        mDhcpRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mConnectionTypeSelect = CONNECT_TYPE_DHCP;
                mStaticIpConfigLinearLayout.setVisibility(View.GONE);
            }
        });

        mStaticIpRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mConnectionTypeSelect = CONNECT_TYPE_STATIC;
                mStaticIpConfigLinearLayout.setVisibility(View.VISIBLE);
            }
        });
        p.mPositiveButtonText = getString(com.android.internal.R.string.ok);
        p.mPositiveButtonListener = this;
        p.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
        p.mPositiveButtonListener = this;
        p.mOnPrepareListViewListener = this;
        p.mTitle = getString(R.string.ethernet_dialog_title);
        setupAlert();
    }

    //旋轉屏幕時不會onCreate()
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

 Step2:

@Override
    public void onClick(DialogInterface dialog, int which) {
        boolean positiveResult = which == DialogInterface.BUTTON_POSITIVE;
        if (positiveResult) {
            getEthernetConfigData();
        }else{

        }
    }

 

 

Step3:

 

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="300sp"
         android:layout_height="wrap_content"
         android:fadeScrollbars="false">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="8dp"
            android:text="@string/ethernet_devices_title"/>
        <Spinner
            android:id="@+id/ethernet_devices"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:prompt="@string/ethernet_eth0"
            android:entries="@array/ethernet_device"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="8dp"
            android:text="@string/connection_type_title"
            />
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/dhcp_title"
                android:checked="true"
                android:id="@+id/ethernet_dhcp"
                />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/static_ip_title"
                android:id="@+id/ethernet_static_ip"
                />
        </RadioGroup>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/static_ip_config"
            android:orientation="vertical"
            android:visibility="gone"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/ip_address_title"
                android:layout_marginLeft="18dp"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/ip_address_input"
                android:hint="@string/hint_ip"
                android:inputType="phone"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/gateway_title"
                android:layout_marginLeft="18dp"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_gateway"
                android:id="@+id/gateway_input"
                android:inputType="phone"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/submask_title"
                android:layout_marginLeft="18dp"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_submask"
                android:id="@+id/submask"
                android:inputType="phone"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/proxy_title"
                android:layout_marginLeft="18dp"
                />
            <Spinner
                android:id="@+id/ethernet_proxy"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:prompt="@string/proxy_none"
                android:entries="@array/ethernet_static_proxy"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/dns1_title"
                android:layout_marginLeft="18dp"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_dns1"
                android:id="@+id/dns1_input"
                android:inputType="phone"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/dns2_title"
                android:layout_marginLeft="18dp"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_dns2"
                android:id="@+id/dns2_input"
                android:inputType="phone"
                />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

 

Step4:

在AndroidManifest.xml裏添加Activity的聲明:

 

<activity android:name=".network.EthernetConfigDialog"
            android:theme="@*android:style/Theme.DeviceDefault.Settings.Dialog"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:enabled="true"
            android:excludeFromRecents="true">
</activity>

  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics