官术网_书友最值得收藏!

Creating buttons and capturing click events

In any given app, you'll notice that creating buttons and capturing their click events is one of the most common tasks you do. This recipe will show you how to declare a button control in Titanium and attach a click event to it. Within that click event, we'll perform a task and log it to the info window in Appcelerator Studio.

This recipe will also demonstrate how to implement some of the default styling mechanisms available for you via the API.

How to do it...

Open your app.js file and type the following code. If you're following along with the LoanCalc app, the following code should go after you created and added the textfield controls:

//calculate the interest for this loan button
var buttonCalculateInterest = Ti.UI.createButton({
   title: 'Calculate Total Interest',
  id: 1,
  top: 10
});

//add the event listener
buttonCalculateInterest.addEventListener('click', calculateAndDisplayValue);

//add the first button to our view
view.add(buttonCalculateInterest);

//calculate the interest for this loan button
var buttonCalculateRepayments = Ti.UI.createButton({
   title: 'Calculate Total Repayment',
  id: 2,
  top: 10
});

//add the event listener
buttonCalculateRepayments.addEventListener('click', 
                          calculateAndDisplayValue);

//add the second and final button to our view
view.add(buttonCalculateRepayments);

Now that we've created our two buttons and added the event listeners, let's create the calculateAndDisplayValue() function to do some simple fixed interest mathematics and produce the results, which we'll log to the Appcelerator Studio console:

//add the event handler which will be executed when either of //our calculation buttons are tapped
function calculateAndDisplayValue(e)
{
   //log the button id so we can debug which button was tapped
  console.log('Button id = ' + e.source.id);

    if (e.source.id == 1) 
    {
       //Interest (I) = Principal (P) times Rate Per Period 
       //(r) times Number of Periods (n) / 12 
       var totalInterest = (tfAmount.value * (interestRate / 
       100) * numberMonths) / 12;
        
      //log result to console
      console.log('Total Interest = ' + totalInterest);
    }
    else 
    {
      //Interest (I) = Principal (P) times Rate Per Period (r) 
      //times Number of Periods (n) / 12 
      var totalInterest = (tfAmount.value * (interestRate / 
      100) * numberMonths) / 12;
      
      var totalRepayments = Math.round(tfAmount.value) +  
      totalInterest;
      
      //log result to console
      console.log('Total repayments' + totalRepayments);
    }

} //end function

How it works...

Most controls in Titanium are capable of firing one or more events, such as focus, onload, or (as in our recipe) click. The click event is undoubtedly the one you'll use more often than any other. In the preceding source code, you will notice that, in order to execute code from this event, we are adding an event listener to our button, which has a signature of click. This signature is a string and forms the first part of our event listener. The second part is the executing function for the event.

It's important to note that other component types can also be used in a similar manner; for example, an imageview can be declared. It can contain a custom button image, and can have a click event attached to it in exactly the same way as a regular button can.

主站蜘蛛池模板: 浦东新区| 武功县| 定襄县| 寻乌县| 乌审旗| 永平县| 淳安县| 无棣县| 兴业县| 湘乡市| 泗阳县| 云林县| 青河县| 仁化县| 临邑县| 阿克苏市| 清远市| 舞钢市| 大城县| 琼结县| 昌邑市| 淳化县| 三门县| 伊宁县| 云霄县| 阜康市| 赤峰市| 衡阳县| 太白县| 怀来县| 潞西市| 裕民县| 深水埗区| 娄烦县| 抚松县| 阿拉善左旗| 扶绥县| 余庆县| 青铜峡市| 卓资县| 石河子市|