日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android基础组件----Button的使用

發布時間:2023/12/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android基础组件----Button的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  按鈕由文本或圖標(或文本和一個圖標)組成,當用戶觸摸到它時,會發生一些動作。今天我們開始Button的學習。少年的愛情永遠不夠用,一杯酒足以了卻一件心事。

?

Button的簡要說明

根據你是否想要一個帶有文本的按鈕,一個圖標,或者兩者,你可以在三種方式中創建按鈕來進行布局:

  

  • With text, using the Button class:
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_text"... />
  • With an icon, using the ImageButton class:
<ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/button_icon"... />
  • With text and an icon, using the Button class with the android:drawableLeft attribute:
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_text"android:drawableLeft="@drawable/button_icon"... />

?

按鈕的響應

1) xml中定義的Button中增加屬性android:onClick

  • 在xml中定義android:onClick屬性:
<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send"android:onClick="sendMessage" />
  • 在MainActivity中定義sendMessage方法:
public void sendMessage(View view) {// Do something in response to button click }

2) 在代碼中使用OnClickListener

Button button = (Button) findViewById(R.id.button_send); button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// Do something in response to button click } });

?

按鈕樣式

1) Borderless button : To create a borderless button, apply the borderlessButtonStyle style to the button.

<Buttonandroid:onClick="sendMessage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="bordless button"style="?android:attr/borderlessButtonStyle" />

2) Custom background: Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button's current state

  • Create three bitmaps for the button background that represent the default, pressed, and focused button states.?
  • Place the bitmaps into the res/drawable/ directory of your project. Be sure each bitmap is named properly to reflect the button state that they each represent.

  

  • Create a new XML file in the res/drawable/ directory (name it something like button_custom.xml). Insert the following XML
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/button_pressed"android:state_pressed="true" /><item android:drawable="@drawable/button_focused"android:state_focused="true" /><item android:drawable="@drawable/button_default" /> </selector>
  • Then simply apply the drawable XML file as the button background
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="send button"android:background="@drawable/button_custom" />

?

測試的項目

?項目結構:

MainActivity.java:

package com.huhx.linux.buttontest;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.sendButton);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "button click", Toast.LENGTH_SHORT).show();}});}// bordless buttonpublic void sendMessage(View view) {Toast.makeText(MainActivity.this, "Borderless Button", Toast.LENGTH_SHORT).show();} }

?button_custom.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/button_pressed"android:state_pressed="true" /><item android:drawable="@drawable/button_focused"android:state_focused="true" /><item android:drawable="@drawable/button_default" /> </selector>

?activity_main.xml.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical"tools:context="com.huhx.linux.buttontest.MainActivity"><Buttonandroid:id="@+id/sendButton"android:text="text button"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ImageButtonandroid:src="@mipmap/ic_launcher"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:drawableRight="@mipmap/ic_launcher"android:text="Linux"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:onClick="sendMessage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="bordless button"style="?android:attr/borderlessButtonStyle" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="send button"android:background="@drawable/button_custom" /> </LinearLayout>

?運行效果如下:

友情鏈接

?

轉載于:https://www.cnblogs.com/huhx/p/baseuseandroidbutton.html

總結

以上是生活随笔為你收集整理的android基础组件----Button的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。