【问题标题】:Multiple buttons with different layouts具有不同布局的多个按钮
【发布时间】:2014-11-04 04:41:02
【问题描述】:

我有一个带有 5 个按钮的活动。每个按钮应该显示一个活动(总共 5 个不同的活动)。我为多个按钮尝试了很多方法,但我的应用程序不起作用。活动的第一个按钮启动它的链接活动没有任何问题(btEtiologia 和活动是 Etiology),但第二个按钮仍然不会显示,并返回到主活动(即主布局)。当我重新启动应用程序时,我尝试使用第二个按钮和第三个按钮并正常工作,但第四个按钮不会显示并返回到主要活动。我已经在三星 Galaxy S2 上下载了该应用程序,并且与模拟器相同。 ¿ 我的代码有什么问题吗?

这是Inicio的java代码:

package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Inicio extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inicio);
    Button btInformacion = (Button) findViewById(R.id.btInformacion);
    btInformacion.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Toast.makeText(getBaseContext(), "Esta aplicación esta basada en el manejo de las infecciones de prótesis articulares de la IDSA", Toast.LENGTH_SHORT).show();
        }
    });

        findViewById(R.id.btEtiologia).setOnClickListener(this);
        findViewById(R.id.btAlgoritmo).setOnClickListener(this);
        findViewById(R.id.btAntibioiv).setOnClickListener(this);
        findViewById(R.id.btAntibioral).setOnClickListener(this);
        findViewById(R.id.btRetirada).setOnClickListener(this);

    }

    public void onClick(View arg0){

        // create a general intent
        Intent intent = null;
        // define an intent for all cases
        switch(arg0.getId()){
            case R.id.btEtiologia:
                // Setting intent for first button
                intent = new Intent(this,Etiology.class);
                break;
            case R.id.btAlgoritmo:
                // Setting intent for second button
                intent = new Intent(this,Algoritmo.class);
                break;
            case R.id.btAntibioiv:
                // Setting intent for third button
                intent = new Intent(this,Antibioiv.class);
                break;
            case R.id.btAntibioral:
                // Setting intent for fourth button
                intent = new Intent(this,Antibioral.class);
                break;
            case R.id.btRetirada:
                // Setting intent for fourth button
                intent = new Intent(this,Retirada.class);
                break;              
}         
this.startActivity(intent); 
                    }           
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.inicio, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

主要活动代码为:

package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btInicio = (Button) findViewById(R.id.btInicio);
    btInicio.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), Inicio.class);
                startActivityForResult(intent, 0);
        }
        });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

【问题讨论】:

  • 嗨,我已经在我的示例应用程序中执行了相同的代码模式,以查看它是否有效,但它对我来说非常有效包含 5 个按钮的主要活动每个需要 5 个不同的活动。如果我的猜测是正确的,请检查每个活动中的 setcontentview,您已将哪个 xml 文件提供给活动以加载 i。如果仍然无法正常工作,请尝试在我的电子邮件 mahesh.venkat27@gmail.com 上分享您的代码,我会尝试修复它。
  • 非常感谢希德。我检查了每个活动的代码,但我找不到问题。感谢您的帮助。

标签: java android button layout android-activity


【解决方案1】:

根据您的代码,您的图像将代码拖到 OutOfMemory 异常。虽然您使用的图像是 120 dpi,但它们的尺寸是 640*480 的两倍。请更改图像大小,然后问题将得到解决。我已经更改了所有 5 种图像尺寸,现在我可以通过单击按钮导航到所有屏幕。在 ldpi 文件夹中将图像设置为 640*480 左右。

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2018-11-24
    • 2015-10-05
    • 1970-01-01
    • 2014-07-17
    相关资源
    最近更新 更多