I have 6 buttons. Each has a different text on it. Pushing any button calls the one same method which changes the text and changes button positions between them. I can't figure out how to get the text from a specific button when it pushed. I can only return either random button text or all of them. Somewhat like this:
public void ReadFromButton()
{
Button button;
button = GetComponent<Button>();
var koba = button.GetComponentInChildren<Text>().text;
Debug.Log(koba);
}
So I need any advice. Thanks.
buttons
You can add an int to your button click function like ReadFromButton(int i) and then in unity give your buttons numbers in onclickevent, according to your public Text[] buttontexts
Like:
public Text[] buttontexts;
public void ReadFromButton(int i)
{
var koba = buttontexts[i].text;
Debug.Log(koba);
}
But i believe there is more ways to do that.
Related
in Unity 5- I am trying to disable a ui.button- once it is clicked. There is a list of buttons- among which- the clicked one will be disabled. But I am not sure, if it is possible to get the event triggering gameobject.
Unity Editor-
Code:
// called from ui.button clicks
public void callThisMethod(string param) {
// how to get the clicked button gameobject here
}
Just Add another event to return the button
public Button ButtonPressed(Button ThisButton)
{
return ThisButton;
}
You can add something like this to your code:
public Button yourButton;
public void yourButtonClicked(){
yourButton.interactable = false;
}
Assign your button to the Button object in inspector. Then where it says "On Click()" in the image you have above, select the script the above code is added to, and select the "yourButtonClicked()" function. That will disable the button once it is clicked.
To get your clicked button game object, you can use: EventSystem.current.currentSelectedGameObject
Pass the gameObject of the button as a parameter while your are adding event listener to the UI button.
Hope that helps:
GameObject myButtonGameObject = myButton.gameObject;
myButton.onClick.AddListener(() => {LogName(myButtonGameObject); });
public void LogName(GameObject buttonGameObject = null){
Debug.Log(myButtonGameObject);
}
Note: The given approach is useful, when you need to dynamically show the clicked buttons name, but if you know the exact button, you can make a public field for the myButton and get the name without passing any parameters.
I want to give the filter button a right click function that when I right click it, it will clear all the grid criteria.
setFilterOnKeypress(false);
setFilterByCell(true);
setFilterButtonPrompt("Left click to filter, right click to clear all texts.");
Button button = new Button();
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
if (event.isRightButtonDown())
{
SC.warn("right clicked");
clearCriteria();
}
}
});
setFilterButtonProperties(button);
This is not working, any ideas on why it isnt working?
Keep the functionality separate. There is no meaning of mixing two different tasks on the same button. Think from the end user's perspective.
Read more on your another post Pass a handler to filter button property that is some what asked in the same context.
LinearLayout cLinearLayout = (LinearLayout) findViewById(R.id.col_linearlayout);
cLinearLayout.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener()
{
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
if (v == cLinearLayout) {
System.out.println("CompID1::" + menu.size());
System.out.println("Rowcount:"+ cLinearLayout.getChildCount());
}
});
}
In this code I am able to get only linear layout id. I want to get id of edit box that created inside that linear layout, how can I do that?
Make your EditText to final .
so it can be accessible inside your linear.
Currently, I have a TabLayoutPanel with a few tabs, inside of each tab is a set of breadcrumbs. I would like to be able to display my breadcrumbs right next to the tabs (inside the TabBar itself). I haven't yet seen any instance of someone having done this, and I'm beginning to believe I might end up rewriting their TabLayoutPanel class myself and implementing that where needed, but obviously I'd rather not go that route unless there's no alternative.
Anyone have any guidance on this?
Just came across the same problem. Here are most of the relevent code snippets. I added a Unicode arrow when the tab was selected, and removed it when the tab was deselected.
private final String htmlStr ="\u25bb";
private String getTabTitle(String html){
// Designed to work for this example input.
//If you pass a different string you will need to do different matching.
if(html.indexOf("\u25bb") < 0)
return html;
else
return html.substring(html.indexOf("\u25bb")+1);
}
#Override
public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
if(!getSelectedIndex().equals(event.getItem())) {
notifyCurrentTabOfClosing();
selectedIndex = event.getItem();
String tabtitle = getTabTitle(getTabBar().getTabHTML(selectedIndex));
getTabBar().setTabHTML(selectedIndex, htmlStr+tabtitle);
}
}
}
public void selectTab(Widget widget) {
int widgetIndex = getWidgetIndex(widget);
selectTab(widgetIndex);
String tabtitle = getTabTitle(getTabBar().getTabHTML(widgetIndex));
getTabBar().setTabHTML(widgetIndex, htmlStr+tabtitle);
}
I'm using GWT 1.6.
I am creating a panel that contains a Button and a Label, which I then add to a FlexTable as one of its cells.
The Button is not receiving any Click events. I see that the table supports determining which Cell is clicked on, but in this case, I want the Mouse events to propagate to the various widgets inside the cell. Any idea on how to do that?
Yeah, I hit that, too - no widgets in the table will receive events. I ended up using code like this:
FixedWidthGrid dataTable = createDataTable();
...
dataTable.addTableListener(new TableListener() {
public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
storyViewer.showStory(table.getRowValue(row));
}
});
You could probably start with something like that, then programmatically send events to your button widget to make the appearance of clicking.
If you know how big your table will be use a Grid instead. All of your widgets will receive there events. I have done this and created my own sortable table.
You have to subclass the Button google Class and add a constructor with two additional arguments (int col, int row).
e.g.
public class RuleButton extends Button {
private int row;
private int col;
public RuleButton(String html, ClickListener listener, int row, int col) {
super(html, listener);
setRow(row);
setCol(col);
}
// getters and setters for row and col attributes.
}
When adding the button, call this constructor and pass row and col indexes to it.