【问题标题】:How to use a method from other class Java Android onClick? [closed]如何使用另一个类 Java Android onClick 中的方法? [关闭]
【发布时间】:2015-08-22 12:53:40
【问题描述】:

有没有办法在buttononClick 中调用另一个class 的方法?

我需要在onClick 中调用myClass.myMethod(),但在主目录中说不

我的 onClick 按钮 android:onClick="textToVoice.listenVoice"

我的课,还没完:

public class TextToVoice extends MainActivity{
public void listenVoice(){
    this.makeVoice();

}
public void downloadVoice(){


}
private void makeVoice(){
    Toast toast = Toast.makeText(getApplicationContext(), this.makeUrl(),
            Toast.LENGTH_SHORT);
    toast.show();
}
private String makeUrl(){
    Editable phstr = super.textVoice.getText();
    String textUrl = phstr.toString();
    if (textUrl.contains(" ")){
        textUrl = textUrl.replace(" ", "+");
    }
    return textUrl;
}
}

还有主要的:

public class MainActivity extends ActionBarActivity {
protected EditText textVoice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);
    this.textVoice =
            (EditText) findViewById(R.id.textVoice);
    TextToVoice textToVoice = new TextToVoice();
}
......

【问题讨论】:

    标签: java android eclipse android-studio


    【解决方案1】:
    class MainActivity extends Activity {
    protected EditText textVoice;
    Button button;
    TextToVoice textToVoice;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ln_dialog);
        textVoice = (EditText) findViewById(R.id.textVoice);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textToVoice = new TextToVoice(MainActivity.this,textVoice.getText().toString());
                textToVoice.listenVoice();
            }
        });
    
    }
    
    
    public class TextToVoice extends MainActivity {
        String textString;
        Context context;
    
        public TextToVoice(Context c ,String s) {
            textString = s;
            context = c;
        }
    
        public void listenVoice() {
            makeVoice();
        }
    
        public void downloadVoice() {
        }
    
        public void makeVoice() {
            Toast.makeText(context, makeUrl(), Toast.LENGTH_SHORT).show();
        }
    
        private String makeUrl() {
    
            if (textString.contains(" ")) {
                textString = textString.replace(" ", "+");
            }
            return textString;
        }
    }
    

    【讨论】:

    • 对不起,我没有告知很多细节,但这里是这样
    • 所以它是否适合你?
    • 没有。我用更多信息编辑了帖子
    • 现在检查它是否适合您。
    • 我已将 ln_dialog 更改为 activity_main,但崩溃了...
    【解决方案2】:

    为什么您的 TextToVoice 类会扩展 MainActivity。 Android 按钮需要 View.OnClickListener 接口的实现。尝试实现上述接口而不是扩展 MainActivity。将类的上下文传递给构造函数并在指定的方法中工作。看看它是否有效。

    【讨论】:

      【解决方案3】:

      将其他类中的方法声明为 public 和 static,然后您可以将其用作 YourClass.method()

      【讨论】:

      • 但我需要使用这个。参考其他方法...现在如何?
      猜你喜欢
      • 2016-06-14
      • 2014-03-15
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多