如何添加一个可点击的按钮,一个TextView是目前点击 [英] How do I add a clickable button to a textview that is clickable currently
问题描述
新到Android试图找出一些东西,任何帮助AP preciated.I有一个TextView这是目前点击。我只是需要把一个可点击按钮的背景吧。这是我的xml文件:
在布局mainscreen.xml
New to android trying to figure something out, any help appreciated.I have a textview which is currently clickable. I just need to put a clickable button background to it. This is my xml file: mainscreen.xml under layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@android:drawable/divider_horizontal"
android:orientation="vertical"
android:showDividers="middle" >
<TextView
android:id="@+id/mainscreen_option"
style="@style/TextView.MainscreenItem"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="1dp"
android:clickable="true"
android:onClick="onMainscreenClicked"
android:text="@string/nav_option"
/>
</LinearLayout>
和在那里我已经定义的按钮选择类code是绘制文件夹下:
bg_button.xml
and the selector class code where i have defined the button is under drawable folder : bg_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/menu_btn_active"/>
<item android:state_pressed="true" android:drawable="@drawable/menu_btn_active" />
<item android:drawable="@drawable/menu_btn" />
</selector>
和相应的Java code为TextView的是:
mainscreennav.java
and the corresponding javacode for the textview is : mainscreennav.java
private void highlightMenuItem(){
TextView highlightedTextView = null;
final String activeFragmentTitle = getArguments().getString(ACTIVE_MENU_ITEM);
final Resources resources = Application.getAppResources();
if (resources.getString(R.string.nav_option_mainscreen).equals(FragmentTitle)) {
highlightedTextView = (TextView) getView().findViewById(R.id.nav_option_mainscreen);
} highlightedTextView.setTextColor(getResources().getColor(R.color.dark_orange));
}
}
任何人都可以指导我,我怎么能这样的TextView转换成一个按钮,使得两者都是可点击在一起,从左至右某些DP的我可以设置文本边距的按钮。
提前致谢!贾斯汀
Can anyone guide me as to how I can convert this textview into a button such that both are clickable together and i can set the text margin in the button from left as certain dp's. Thanks in advance! Justin
推荐答案
编辑:
创建您的code选择器的XML,并在您绘制文件夹中。我将创建一个btn_custom.xml
create a selector xml with your code and put in your drawable folder. I'll create a btn_custom.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/btn_active" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_active" />
<item
android:drawable="@drawable/btn_default" />
</selector>
然后,在你的TextView的:
Then, on your TextView:
<TextView android:text="MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:clickable="true"
android:background="@drawable/btn_custom"
android:padding="10dp"/>
在填充,您调整按钮填充。
On padding, you adjust your button padding.
在后台,把你选择的名称(在我的情况,@绘制/ btn_custom)
On background, put the name of your selector (in my case, @drawable/btn_custom)
和你的影响已经在
比,只注册一个onClickListener
Than, just register an onClickListener
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTextButton = (TextView) findViewById(R.id.my_button_id);
myTextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "ButtonClick", 200).show();
}
});
}
PS:你可以用你的onClick方法来代替:P
Ps: You can use your onClick method instead :P
永远记住:一个按钮只是一个病急乱投医的TextView
Always remember: A Button is just a "styled" TextView.
这是Android源$ C $ C为Button类:
This is the Android source code for Button class:
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
是的,它只是
这篇关于如何添加一个可点击的按钮,一个TextView是目前点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!