简体   繁体   English

为Android的滚动视图添加视图

[英]Adding views to scroll view for Android

I'll start by explaining what the finished program should do. 我将从解释完成的程序应该做什么开始。 First, I would like it to set a OnClickListener for a specific button. 首先,我希望它为特定按钮设置OnClickListener。 Once the button is clicked, I will assume that user has entered something into the EditText. 单击该按钮后,我将假定用户已在EditText中输入了一些内容。 I read in what the user has typed into the EditText, than I check to see if it is a specific string. 我读了用户在EditText中键入的内容,然后检查它是否是特定的字符串。 If it is, than I add a TextView than set the text of that TextView to ask the user what number he would like to call. 如果是这样,那么我要添加一个TextView,然后设置该TextView的文本以询问用户他想拨打的电话号码。 I add a EditText for the user to enter data into, than a button. 我添加了一个EditText供用户输入数据,而不是一个按钮。 Once the user has entered data into that EditText and has clicked the button, I want to create a new object called theCall, and the app makes an intent from within the class phone. 用户将数据输入到该EditText中并单击按钮后,我想创建一个名为theCall的新对象,然后该应用程序从类电话中发出一个意图。 Here is my current code: 这是我当前的代码:

Main Class: 主类:

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());
                    }

                });
            }
        }
    });

}
}

phone class: 电话类别:

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:

<?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>

My problem is, when I enter "call" into the initial EditText, nothing happens. 我的问题是,当我在初始EditText中输入“ call”时,什么也没发生。

Any help is appreciated! 任何帮助表示赞赏!

Thanks, 谢谢,

Sam 山姆

You need to add convo to your root LinearLayout. 您需要将convo添加到您的根LinearLayout中。 Perhaps like this inside your onClick() method: 也许在您的onClick()方法中是这样的:

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

Also you cannot use == to compare Strings in Java, you must use equals() : 另外,您不能使用==来比较Java中的字符串,必须使用equals()

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

(For more information on why please read: How do I compare strings in Java? .) (有关为什么的更多信息,请阅读: 如何比较Java中的字符串? 。)

try following : 尝试以下:

  1. Use try catch, 使用try catch,

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

  2. Add <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> to your manifest. <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>到清单中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM