【问题标题】:Android Eclipse: trouble with button click codeAndroid Eclipse:按钮点击代码的问题
【发布时间】:2014-05-15 21:02:30
【问题描述】:

我首先尝试在 Eclipse 上制作一个简单的应用程序。 我的目标是创建一个简单地调用数字的按钮。 但是,看了很多教程之类的,我还是不知道在哪里写代码,什么代码最好。

这是我的清单。我插入使用权限 CALL_PHONE 因为它是必需的(正如我从一些教程中了解到的)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.projectrandomfox.randomfox"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

   <uses-permission android:name="android.permission.CALL_PHONE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.projectrandomfox.randomfox.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我的activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select VAS"
    android:textSize="40dp"
    android:layout_gravity="center" />

<Button
    android:id="@+id/balance"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="Check Balance"
    android:layout_gravity="center"
     /> </LinearLayout>

这是我的活动 java

package com.projectrandomfox.randomfox;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

}

就是这样。 请帮我弄清楚在活动中在哪里写代码,以及写什么代码 这对我来说意义重大:)

【问题讨论】:

    标签: java javascript android eclipse button


    【解决方案1】:

    在你的 onCreate 方法中添加这个:

    Button button = (Button) findViewById(R.id.balance);
    
    myButton.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View view)
        {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + telephoneNumber));
            startActivity(intent);
        }
    }
    

    所有你需要做的就是设置电话号码变量

    【讨论】:

      【解决方案2】:

      首先,您必须获得您的 Button 的引用。

      Button myButton = findViewById(R.id.balance);
      

      然后添加 onClickListener。

      myButton.setOnClickListener(new OnClickListener()
      {
          @Override
          public void onClick(View view)
          {
              // Do whatever
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        • 2013-05-18
        • 1970-01-01
        • 1970-01-01
        • 2015-05-09
        相关资源
        最近更新 更多