多个按钮具有不同的布局

时间:2014-09-10 13:39:24

标签: java android button layout android-activity

我有一个带5个按钮的活动。每个按钮应显示一个活动(共5个不同的活动)。我为多个按钮尝试了很多方法但我的应用程序不起作用。活动的第一个按钮开始其链接的活动没有任何问题(btEtiologia和活动是病因学),但第二个按钮仍然不会显示,并返回到主活动(即家庭布局)。当我重新启动应用程序时,我尝试使用第二个按钮,第三个按钮和工作,但第四个按钮不会显示并返回主活动。我已经在三星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);
}
}

1 个答案:

答案 0 :(得分:1)

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

相关问题