Android 小控件使用合集

冒泡排序

public class bubbleSort {  
public  bubbleSort(){  
     int a[]={49,38,65,97,**,**,**,**,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
    int temp=0;  
    for(int i=0;i<a.length-1;i++){  
        for(int j=0;j<a.length-1-i;j++){  
        if(a[j]>a[j+1]){  
            temp=a[j];  
            a[j]=a[j+1];  
            a[j+1]=temp;  
        }  
        }  
    }  
    for(int i=0;i<a.length;i++)  
    System.out.println(a[i]);     
}  
} 
public void popwindow(){
        v = View.inflate(context, R.layout.item_cha,null);
        pop = new PopupWindow(v, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 设置PopupWindow的背景
        pop.setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
        // 设置PopupWindow是否能响应外部点击事件
        pop.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        pop.setTouchable(true);

    }

点击控件显示popupwindow

 ImageView cha2=(ImageView) convertView.findViewById(R.id.cha2);
            cha2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popwindow();
                    pop.showAsDropDown(view);
                    final Button btnquxiao=(Button) v.findViewById(R.id.quxiao);
                    btnquxiao.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            pop.dismiss();
                        }
                    });

ActionBar

ActionBar mActionBar=getSupportActionBar();
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setTitle("设置");

夜间模式

public void yejian(){
        int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        if(mode == Configuration.UI_MODE_NIGHT_YES) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        } else if(mode == Configuration.UI_MODE_NIGHT_NO) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            // blah blah
        }

        recreate();
    }

新建夜间模式资源文件夹:在res目录下新建values-night文件夹,然后在此目录下新建colors.xml文件在夜间模式下的应用的资源。当然也可以根据需要新建drawable-night,layout-night等后缀为-night的夜间资源文件夹。
我的values和values-night目录下的colors.xml的内容如下:

<?xml version="1.0" encoding="utf-8"?>
 <resources>
     <color name="colorPrimary">#35464e</color>
     <color name="colorPrimaryDark">#212a2f</color>
     <color name="colorAccent">#212a2f</color>
     <color name="textColorPrimary">#616161</color>
     <color name="viewBackground">#212a2f</color>
 </resources>

让我们自己的主题继承并应用DayNight主题。

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">

         <item name="colorPrimary">@color/colorPrimary</item>
         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
         <item name="colorAccent">@color/colorAccent</item>
         <!--customize your theme here-->

 </style>

短信验证 我用的是mob首先去mob官网下载smsssdk,下载完之后会有俩文件夹
点击这个是mob网址
这里写图片描述

之后是打开
之后是打开

在完后就是
这里写图片描述

下面是代码展示

public class MainActivity extends AppCompatActivity {
    private EditText editphone;
    private EditText edityanzheng;
    private Button btnhuoqu;
    private Button btnyanzheng;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editphone = (EditText) findViewById(R.id.editphone);
        edityanzheng = (EditText) findViewById(R.id.edityanzheng);
        btnhuoqu = (Button) findViewById(R.id.btnhuoqu);
        btnyanzheng = (Button) findViewById(R.id.btnyanzheng);
        SMSSDK.registerEventHandler(eh);
        btnhuoqu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SMSSDK.getVerificationCode("86", editphone.getText().toString().trim(), new OnSendMessageHandler() {
                    @Override
                    public boolean onSendMessage(String s, String s1) {
                        return false;
                    }
                });
            }
        });
        btnyanzheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SMSSDK.submitVerificationCode("86", editphone.getText().toString().trim(), edityanzheng.getText().toString().trim());
            }
        });
    }
    EventHandler eh = new EventHandler() {

        @Override
        public void afterEvent(int event, int result, Object data) {

            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码正确的回调
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
            } else {
                ((Throwable) data).printStackTrace();
                Log.e("tag", ((Throwable) data).getMessage().toString());
                //获取验证码成功
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();

                    }
                });
            }

        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eh);
    }
}

复制代码
复制代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="手机号"
        android:id="@+id/editphone"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="验证码"
        android:id="@+id/edityanzheng"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnhuoqu"
        android:text="获取验证码"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnyanzheng"
        android:text="验证"/>



</LinearLayout>

复制代码
复制代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shilongxinduanxin">
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme"
        android:name="com.mob.MobApplication">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.mob.tools.MobUIShell"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="stateHidden|adjustResize"/>
        <meta-data android:name="Mob-AppKey" android:value="20c5ba6bb818f"/>
        <meta-data android:name="Mob-AppSecret" android:value="1965928e2b6e6ff7956332ef83b06d52"/>
    </application>

</manifest>
 buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories{
    flatDir{
        dirs 'libs' //就是你放aar的目录地址
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile files('libs/MobCommons-2017.0607.1736.jar')

    compile name:'SMSSDK-3.0.0',ext:'aar'
    compile name:'SMSSDKGUI-3.0.0',ext:'aar'
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值