2012-12-26 42 views
0

我將首先解釋完成的程序應該執行的操作。首先,我希望爲特定的按鈕設置一個OnClickListener。點擊按鈕後,我會假定用戶已經在EditText中輸入了一些內容。我讀了用戶輸入到EditText中的內容,而不是檢查它是否是特定的字符串。如果是這樣,比添加一個TextView而不是設置該TextView的文本來詢問用戶他想要調用的是什麼數字。我爲用戶添加一個EditText來輸入數據,而不是按鈕。一旦用戶輸入了該EditText的數據並點擊了該按鈕,我想要創建一個名爲theCall的新對象,並且該應用程序會在班級電話中製作一個意圖。這裏是我當前的代碼:將視圖添加到Android的滾動視圖

主類:

package com.sam.projectDAD; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnFocusChangeListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class ProjectDADActivity extends Activity { 
/** Called when the activity is first created. */ 

private EditText functionName,input; 
private Button subCommand,subParam; 
private TextView response; 
private LinearLayout convo; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    response = new TextView(this); 
    convo = new LinearLayout(this); 
    input = new EditText(this); 
    subParam = new Button(this); 

    functionName = (EditText) findViewById(R.id.functionName); 
    subCommand = (Button) findViewById(R.id.unFocus); 

    subCommand.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      String functionNameText = functionName.getText().toString(); 
      if (functionNameText == "call") { 
       convo.addView(response); 
       response.setText("What phone number would you like to call?"); 
       convo.addView(input); 
       convo.addView(subParam); 
       subParam.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
          phone theCall = new phone(response.getText().toString()); 
        } 

       }); 
      } 
     } 
    }); 

} 
} 

手機類:

package com.sam.projectDAD; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.view.View; 
import android.view.View.OnFocusChangeListener; 

public class phone extends Activity { 
public phone(String phone){ 
    call(phone); 
} 

private void call(String phoneNum){ 
    String uri = "tel:" + phoneNum.trim() ; 
    Intent intent = new Intent(Intent.ACTION_CALL); 
    intent.setData(Uri.parse(uri)); 
    startActivity(intent); 
} 

} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.33" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <Button 
      android:id="@+id/unFocus" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

<EditText 
    android:id="@+id/functionName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 


    </LinearLayout> 
</ScrollView> 

</LinearLayout> 

我的問題是,當我輸入 「呼」 進最初的EditText,沒有任何反應。

任何幫助表示讚賞!

感謝,

山姆

回答

1

您需要添加convo到根的LinearLayout。也許你這樣的onClick()方法內:

LinearLayout root = (LinearLayout) findViewById(R.id.scrollView1).getParent(); 
root.addView(convo); 

你也無法使用==在Java中比較字符串,則必須使用equals()

if (functionNameText.equals("call")) { 

(關於爲什麼請閱讀更多信息:How do I compare strings in Java?

0

請嘗試以下操作:

  1. 使用嘗試捕捉,

    try { //your code} catch(ActivityNotFoundException activityException) {Log.e("helloandroid dialing example", "Call failed", e); }

  2. 添加 <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> 到您的清單。