android jni示例_Android切换按钮,开关示例

android jni示例

Today we will learn about Android Toggle Button and Switch in android app. We’ll discuss and implement Switch button Widget and the ToggleButton widget in our application.

今天,我们将学习android应用中的Android切换按钮和开关。 我们将讨论并实现切换按钮控件和ToggleButton在我们的应用程序部件。

Android切换按钮 (Android Toggle Button)

Android Toggle Button is used to display on and off state on a button. Switch is another type of toggle button that’s predominantly used since Android 4.0. Android Switch provides a slider control. Both ToggleButton and Switch are subclasses of CompoundButton class.

Android切换按钮用于显示按钮的打开和关闭状态。 开关是开关按钮的另一种类型是由于Android 4.0中主要使用。 Android Switch提供了滑块控件。 ToggleButton和Switch都是CompoundButton类的子类。

XML Attributes used to define a ToggleButton are described below.

下面介绍用于定义ToggleButton的XML属性。

  1. android:disabledAlpha: The alpha to apply to the indicator when disabled

    android:disabledAlpha :禁用时应用于指标的Alpha
  2. android:textOff: The text for the button when it is not checked

    android:textOff :未选中按钮时的文本
  3. android:textOn: The text for the button when it is checked

    android:textOn :按钮被选中时的文本

To modify the ToggleButton programmatically the following methods are used.

要以编程方式修改ToggleButton,请使用以下方法。

  1. CharSequence getTextOff(): It returns the text when button is not in the checked state

    CharSequence getTextOff() :当按钮未处于选中状态时,它返回文本
  2. CharSequence getTextOn(): It returns the text for when button is in the checked state

    CharSequence getTextOn() :返回按钮处于选中状态时的文本
  3. void setChecked(boolean checked): It changes the checked state of this button

    void setChecked(boolean checked) :更改此按钮的检查状态

Android开关 (Android Switch)

Android Switch or SwitchCompat widget ( a part of AppCompat library which provides backward compatibility for lower Android versions upto Android API v7) is a custom On-Off slider which is commonly seen in phone settings.

Android Switch或SwitchCompat小部件(是AppCompat库的一部分,为向下版本的Android版本直至Android API v7提供向后兼容性)是一个自定义的“开-关”滑块,通常在手机设置中看到。

Advantages of Android Switch Widget:

Android Switch Widget的优点

  1. It is the best replacement for checkboxes and RadioButtons

    它是复选框和RadioButtons的最佳替代
  2. Implementation takes only less than a minute

    实施仅需不到一分钟
  3. No need to use much drawables and other resources

    无需使用大量可绘制对象和其他资源

The xml layout of a SwitchCompat is given below:

下面给出了SwitchCompat的xml布局:

android:text is used to display a Text besides the slider switch button.

android:text用于在滑块开关按钮之外显示文本。

Android切换按钮和开关示例 (Android Toggle Button and Switch Example)

In this application we’ll display two ToggleButton and one Switch button. The state of the Toggle Buttons would be displayed in a SnackBar when the FloatingActionButton is pressed. The state of the Switch button is changed to true whenever the action button of the snackbar is clicked. Or the state is displayed in the snackbar by sliding the switch.

在此应用程序中,我们将显示两个ToggleButton和一个Switch按钮。 当按下FloatingActionButton时,切换按钮的状态将显示在SnackBar中。 只要单击快餐栏的操作按钮,“切换”按钮的状态就会更改为true。 或者通过滑动开关在小吃栏中显示状态。

Android切换按钮和开关项目结构 (Android Toggle Button and Switch Project Structure)

Android切换按钮代码 (Android Toggle Button Code)

The activity_main.xml remains the same. The content_main.xml contains two Toggle Buttons and a Switch checked to false by default as shown in the code snippet below :

activity_main.xml保持不变。 content_main.xml包含两个切换按钮和一个默认情况下选中为false的开关,如下面的代码片段所示:




    

    

    


The MainActivity.java is given below:

MainActivity.java如下所示:

package com.journaldev.switchandtoggle;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    ToggleButton tb1, tb2;
    SwitchCompat switchCompat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                StringBuilder result = new StringBuilder();
                result.append("ToggleButton1 : ").append(tb1.getText());
                result.append("\nToggleButton2 : ").append(tb2.getText());

               Snackbar snackbar= Snackbar.make(view, result.toString(), Snackbar.LENGTH_LONG)
                        .setAction("SWITCH ENABLE", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                switchCompat.setChecked(true);
                            }
                        });

                snackbar.setActionTextColor(Color.RED);
                snackbar.show();
            }
        });

        tb1= (ToggleButton)findViewById(R.id.tb1);
        tb2=(ToggleButton)findViewById(R.id.tb2);
        switchCompat=(SwitchCompat)findViewById(R.id.switchButton);

        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                Snackbar.make(buttonView, "Switch state checked "+isChecked, Snackbar.LENGTH_LONG)
                        .setAction("ACTION",null).show();
            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

A String builder is used to get the current state of the toggle buttons and append them to display in the snackbar.
Switch button being a subclass of Compound Button, an OnCheckChangeListener is implemented as shown in the code above.

字符串构建器用于获取切换按钮的当前状态,并将其附加以显示在小吃栏中 。
开关按钮是复合按钮的子类,如上面的代码所示,实现了OnCheckChangeListener

The output below is the app in action.

以下输出是正在运行的应用程序。

This brings an end to android toggle button and switch in android tutorial. You can download the final Android SwitchAndToggle Project from the link below.

这结束了android切换按钮和android教程中的切换。 您可以从下面的链接下载最终的Android SwitchAndToggle项目。

Download Android Switch and Toggle Button Project 下载Android开关和切换按钮项目

Reference: ToggleButton Android Doc

参考: ToggleButton Android文档

翻译自: https://www.journaldev.com/10402/android-toggle-button-switch-example

android jni示例

你可能感兴趣的:(java,android,ios,安卓,python)