无涯教程-Android - ToggleButton函数

这篇具有很好参考价值的文章主要介绍了无涯教程-Android - ToggleButton函数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

ToggleButton将已选中/未选中状态显示为按钮。它基本上是一个带有指示灯的开/关按钮。

Toggle Button

ToggleButton属性

以下是与ToggleButton控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。

Sr.No. Attribute & 描述
1

android:disabledAlpha

这是禁用时应用于指标的Alpha值。

2

android:textOff

这是未选中按钮时的文本。

3

android:textOn

这是选中按钮时的文字。

继承自 android.widget.TextView 类-

Sr.No. Attribute & 描述
1

android:autoText

如果设置,则指定此TextView具有文本输入法,并自动纠正一些常见的拼写错误。

2

android:drawableBottom

这是要在文本下方绘制的图形。

3

android:drawableRight

这是要在文本右侧绘制的图形。

4

android:editable

如果设置,则指定此TextView具有输入法。

5

android:text

这是要显示的文本。

继承自 android.view.View 类-

Sr.No. Attribute & 描述
1

android:background

这是一个可绘制的背景。

2

android:content描述

这定义了简短描述视图内容的文本。

3

android:id

这将为此视图提供一个标识符名称,

4

android:onClick

这是单击该视图时在该视图中要调用的方法的名称。

5

android:visibility

这控制了视图的初始可见性。

示例

本示例将带您完成简单的步骤,以展示如何使用Linear Layout和ToggleButton创建自己的Android应用程序。

以下是修改后的主要Activity文件 src/MainActivity.java 的内容。

package com.example.saira_000.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends ActionBarActivity {
   ToggleButton tg1,tg2;
   Button b1;
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      tg1=(ToggleButton)findViewById(R.id.toggleButton);
      tg2=(ToggleButton)findViewById(R.id.toggleButton2);
      
      b1=(Button)findViewById(R.id.button2);
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            StringBuffer result = new StringBuffer();
            result.append("You have clicked first ON Button-:) ").append(tg1.getText());
            result.append("You have clicked Second ON Button -:) ").append(tg2.getText());
            Toast.makeText(MainActivity.this, result.toString(),Toast.LENGTH_SHORT).show();
         }
      });
   }
}

以下是 res/layout/activity_main.xml 文件的内容-

<RelativeLayout 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"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">
   
   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_above="@+id/imageButton"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="40dp" />
      
   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />
      
   <ToggleButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="On"
      android:id="@+id/toggleButton"
      android:checked="true"
      android:layout_below="@+id/imageButton"
      android:layout_toEndOf="@+id/button2/>
      
   <ToggleButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Off"
      android:id="@+id/toggleButton2"
      android:checked="true"
      android:layout_alignTop="@+id/toggleButton" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/button2"
      android:text="ClickMe"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true" />
      
</RelativeLayout>

以下是 res/values/strings.xml 的内容,以定义这些新常量-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">My Application</string>
</resources>

以下是 AndroidManifest.xml 的默认内容-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.saira_000.myapplication" >
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.My Application.MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
      
   </application>
</manifest>

单击"运行工具栏。 Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将在"Emulator"窗口中显示-

将出现以下屏幕-

Android 中的 ToggleButton函数 - 无涯教程网无涯教程网提供ToggleButton将已选中/未选中状态显示为按钮。它基本上是一个带有指示灯的开/关按钮。...https://www.learnfk.com/android/android-togglebutton-control.html文章来源地址https://www.toymoban.com/news/detail-687416.html

到了这里,关于无涯教程-Android - ToggleButton函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 无涯教程-Android - AutoCompleteTextView函数

    AutoCompleteTextView是一个类似于EditText的视图,只是它在用户键入时自动显示补充数据。 以下是与AutoCompleteTextView控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。 Sr.No Attribute 描述 1 android:completionHint 这定义了在

    2024年02月10日
    浏览(28)
  • 无涯教程-Android - CheckBox函数

    CheckBox是可以由用户切换的on/off开关。为用户提供一组互不排斥的可选选项时,应使用复选框。 以下是与CheckBox控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。 继承自 android.widget.TextView 类- Sr.No Attribute 描述

    2024年02月10日
    浏览(27)
  • 无涯教程-Android - RadioButton函数

    RadioButton有两种状态:选中或未选中,这允许用户从一组中选择一个选项。 本示例将带您完成一些简单的步骤,以展示如何使用Linear Layout和RadioButton创建自己的Android应用程序。 以下是修改后的主要Activity文件 src/MainActivity.java 的内容。 以下是 res/layout/activity_main.xml 文件的内容- 以

    2024年02月10日
    浏览(22)
  • 无涯教程-Android - Absolute Layout函数

    Absolute Layout 可让您指定其子级的确切位置(x/y坐标),绝对布局的灵活性较差且难以维护。 以下是AbsoluteLayout特有的重要属性- Sr.No Attribute 描述 1 android:id 这是唯一标识布局的ID。 2 android:layout_x 这指定视图的x坐标。 3 android:layout_y 这指定视图的y坐标。 本示例将带您完成简单的

    2024年02月10日
    浏览(23)
  • 无涯教程-Android - List View函数

    Android ListView 是垂直滚动列表中显示的视图,使用 Adapter 从列表(如数组或数据库)中获取内容的列表项会自动插入列表中。 适配器(Adapter)实际上是UI组件和将数据填充到UI组件中的数据源之间的桥梁,适配器保存数据并将数据发送到适配器视图,该视图可以从适配器视图获取数

    2024年02月10日
    浏览(25)
  • 无涯教程-Android - Frame Layout函数

    Frame Layout 旨在遮挡屏幕上的某个区域以显示单个项目,通常,应使用FrameLayout来保存单个子视图,因为在子视图彼此不重叠的情况下,难以以可扩展到不同屏幕尺寸的方式组织子视图。 不过,您可以使用android:layout_gravity属性将多个子项添加到FrameLayout并通过将gravity分配给每

    2024年02月10日
    浏览(23)
  • 无涯教程-Android - List fragments函数

    框架的ListFragment的静态库支持版本,用于编写在Android 3.0之前的平台上运行的应用程序,在Android 3.0或更高版本上运行时,仍使用此实现。 List fragment 的基本实现是用于创建fragment中的项目列表 本示例将向您说明如何基于arrayAdapter创建自己的列表片段,在开始编码之前,我将在

    2024年02月11日
    浏览(27)
  • 无涯教程-Android Online Test函数

    Android在线测试模拟了真正的在线认证考试。您将看到基于 Android概念的多项选择题(MCQ),将为您提供四个options。您将为该问题选择最合适的答案,然后继续进行下一个问题,而不会浪费时间。完成完整的考试后,您将获得在线考试分数。 总问题数-20 最长时间-20分钟 Start Test Androi

    2024年02月10日
    浏览(23)
  • 无涯教程-Android - Style Demo Example函数

    下面的示例演示如何将样式用于单个元素。让我们开始按照以下步骤创建一个简单的Android应用程序- 步骤 说明 1 您将使用Android Studio IDE创建一个Android应用程序,并在 com.example.saira_000.myapplication 包下将其命名为 myapplication ,如中所述您好世界Example一章。 2 修改 src/MainActivity.jav

    2024年02月10日
    浏览(23)
  • 无涯教程-Android Intent Standard Extra Data函数

    下表列出了各种重要的Android Intent Standard Extra Data。您可以查看Android官方文档以获取额外数据的完整列表- Sr.No Extra Data Description 1 EXTRA_ALARM_COUNT 用作AlarmManager intents(意图)中的int Extra字段,以告诉正在调用的应用程序intents(意图)释放了多少未决警报。 2 EXTRA_ALLOW_MULTIPLE 用于指示

    2024年02月10日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包