标签: android-button

使用按钮以编程方式更改屏幕方向

我认为这是可实现的,因为屏幕旋转行为可以达到应用程序级别.

android screen-orientation android-button

91
推荐指数
5
解决办法
12万
查看次数

Android:如何处理按钮点击

我在非Java和非Android领域拥有丰富的经验,我正在学习Android.

我对不同领域有很多困惑,其中之一就是如何处理按钮点击.至少有4种方法(!!!),这里简要列出

为了保持一致性,我将列出它们:

  1. View.OnClickListener在活动中拥有该类的成员,并将其分配给将onClickonCreateactivity方法中处理逻辑的实例.

  2. 在'onCreate'活动方法中创建'onClickListener'并使用setOnClickListener将其分配给按钮

  3. 在活动本身中实现'onClickListener'并将'this'指定为按钮的监听器.对于活动按钮很少的情况,应该分析按钮ID以执行适当按钮的'onClick'处理程序

  4. 对实现'onClick'逻辑的活动有公共方法,并将其分配给activity xml声明中的按钮

问题#1:

这些都是方法,还有其他选择吗?(我不需要任何其他的,只是好奇)

对我来说,最直观的方式是最新的方式:它需要输入最少量的代码并且是最可读的(至少对我而言).

虽然,我没有看到这种方法被广泛使用.使用它有什么用处?

问题2:

每种方法的优缺点是什么?请分享您的经验或良好的链接.

欢迎任何反馈!

PS我试过谷歌并找到了一些关于这个话题的东西,但我发现的唯一的东西就是描述"如何"做到这一点,而不是为什么它好或坏.

java android android-button onclicklistener

88
推荐指数
3
解决办法
23万
查看次数

如何以编程方式将焦点设置为按钮小部件?

是否可以将焦点设置为位于布局中某处的按钮小部件?onCreate我的控件/焦点应该以编程方式在该按钮上的活动.

android android-widget android-button

78
推荐指数
2
解决办法
11万
查看次数

Android按钮带有图标和文本

我的应用程序中有一些这样的按钮:

    <Button
        android:id="@+id/bSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="Search"
        android:textSize="24sp" />
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用文本和图标创建相同的按钮.android:drawableLeft对我不起作用(也许会,但我不知道如何设置图标的最大高度).

所以我创建了一个带有ImageView和TextView的LinearLayout,并使其像一个按钮:

    <LinearLayout
        android:id="@+id/bSearch2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_default"
        android:clickable="true"
        android:padding="16dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:adjustViewBounds="true"
            android:maxHeight="30dp"
            android:maxWidth="30dp"
            android:scaleType="fitCenter"
            android:src="@drawable/search_icon" />

        <TextView
            android:id="@+id/tvSearchCaption"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="24sp"
            android:paddingRight="30dp"
            android:gravity="center"
            android:text="Search" />
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的新按钮正是我想要的(字体大小,图标和文本放置).但它看起来不像我的默认按钮:

在此输入图像描述

所以我试着改变我的新Button的背景和文字颜色:

Button Search = (Button) findViewById(R.id.bSearch);
LinearLayout bSearch2 = (LinearLayout) findViewById(R.id.bSearch2);
bSearch2.setBackground(bSearch.getBackground());
TextView tvSearchCaption = (TextView)findViewById(R.id.tvSearchCaption);
tvSearchCaption.setTextColor(bSearch.getTextColors().getDefaultColor());
Run Code Online (Sandbox Code Playgroud)

这给了一个奇怪的结果,我的旧按钮搞砸了:

在此输入图像描述

当我改变XML中这两个按钮的顺序时,所以"新按钮"首先出现,它会产生另一个奇怪的结果:

在此输入图像描述

现在我注意到,当我尝试按下旧按钮时,新按钮会被按下.

有任何想法吗?

android android-layout android-button

77
推荐指数
5
解决办法
19万
查看次数

Android中的圆形按钮

我想在Android程序中创建圆形按钮.我看过 如何用圆角创建EditText?

我想要实现的是:

  1. 圆形边缘按钮
  2. 更改不同状态下的按钮背景/外观(如Onclick,Focus)
  3. 使用我自己的PNG作为背景而不是创建形状.

android rounded-corners android-button

69
推荐指数
5
解决办法
19万
查看次数

如何通过XML和Java代码使用OnClickListener接口?

可能重复:
OnClick()事件和OnClickListener之间的区别?

我是Android开发的半新手,当我第一次开始时,我试图避免以任何必要的方式使用xml布局,因此我之前的一些项目涉及显式创建OnClickListener并将其实现为匿名内部类的按钮.如 -

final Button button = new Button(this);
button.setText("Click to change second line of text");

OnClickListener buttonListener = new View.OnClickListener() {
    boolean clicked = false;
    int numClicks = 0;

    @Override
    public void onClick(View v) {
        if(numClicks > 5) {
            button.setText("STOP IT");
        }
        numClicks++;
        if(clicked == false){
            clicked = true;
            tv2.setText("Text Changed on Button Click");    
        }
        else
        {
            clicked = false;
            tv2.setText("Click again");
        }       
    }
};
button.setOnClickListener(buttonListener);
Run Code Online (Sandbox Code Playgroud)

但随着我越来越熟悉android,我开始理解xml布局的价值,并实现了这样的按钮

    <Button
    android:id="@+id/button1"
    android:layout_height = "wrap_content"
    android:layout_width ="wrap_content"
    android:text = "lets do …
Run Code Online (Sandbox Code Playgroud)

java xml android android-layout android-button

68
推荐指数
1
解决办法
25万
查看次数

setEnabled()vs setClickable(),有什么区别?

到现在为止,当我想阻止用户按下按钮时,我会设置 button.setClickable(false);并通常将文本更改为某种灰色(让用户知道按钮被禁用).今天我偶然发现了这家setEnabled()酒店.

所以我去了文档,看看方法的解释如下:

setEnabled(boolean enabled)
   Set the enabled state of this view.
Run Code Online (Sandbox Code Playgroud)

这甚至意味着什么?启用状态/可点击状态和禁用状态/不可点击状态之间有什么区别?有人可以解释一下我以前做过什么,使用可点击的属性和使用setEnabled()属性之间的区别是什么?什么时候应该用?我搜索了StackOverflow但找不到任何相关内容.提前致谢.

android clickable android-button

68
推荐指数
3
解决办法
6万
查看次数

如何在地图片段API v2布局顶部添加按钮

我正在尝试使用API​​ v2在android中显示地图.
我想要UI这样的东西.但是每当我尝试在布局中添加按钮时它不会反映在输出中
我能够获得没有按钮的地图.
我需要按钮与地图集成,如下面
的Mylayout.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"
tools:context=".MapActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup 
android:id="@+id/radio_group_list_selector"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_weight="1"

>
<RadioButton
android:id="@+id/radioPopular"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/Popular"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/shape_radiobutton"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:textColor="@drawable/textcolor_radiobutton"

/>
<View
android:id="@+id/VerticalLine"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginTop="4dip"
android:background="#aaa"
            />
<RadioButton
android:id="@+id/radioAZ"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/AZ"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/shape_radiobutton2"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:textColor="@drawable/textcolor_radiobutton"

/>
 <View
android:id="@+id/VerticalLine"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:layout_marginTop="4dip"
android:background="#aaa"
            />
<RadioButton
android:id="@+id/radioCategory"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/Category" …
Run Code Online (Sandbox Code Playgroud)

android google-maps android-layout android-button

64
推荐指数
2
解决办法
10万
查看次数

如何从另一个片段打开一个新片段?

我尝试在片段之间进行导航.我有NewFragment.java新的片段工作了.我的问题是:

如何正确onClickListener运行NewFragment.java

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i = new Intent(getActivity(), NewFragment.class);
        startActivity(i);

    }
});
Run Code Online (Sandbox Code Playgroud)

仅供参考:这是来自一个片段(我不知道这是否重要).

android android-fragments android-button onclicklistener

63
推荐指数
4
解决办法
10万
查看次数

Android按钮背景颜色

我试图在我的应用程序中设置按钮的背景颜色,我无法达到我想要的结果...

我试图设置的颜色是holo_green_light(#ff99cc00).为了做到这一点,我正在使用setColorFilter(0xff99cc00, PorterDuff.Mode.MULTIPLY);

我得到的颜色不仅仅是holo_green_lightlightgrey和holo_green_light.

我尝试过使用LightingColorFilter没有太大的成功.

有没有办法以编程方式进行,以便按钮看起来像一个按钮,而不是一个我需要的颜色的扁平矩形.

android button android-button

49
推荐指数
9
解决办法
16万
查看次数