gpt4 book ai didi

java - Android:按钮不起作用

转载 作者:行者123 更新时间:2023-12-01 23:08:27 26 4
gpt4 key购买 nike

这个页面上有 4 个按钮...其中 3 个工作正常,第四个按钮(链接到 ToolsTableLayout.class)根本没有响应。没有错误,应用程序不会崩溃或发生任何事情...您只需单击按钮,什么也不会发生。

按钮类的代码:

public class MainMenu extends Activity implements OnClickListener{

private String result;
boolean isScanout;
public static final String SCAN_RESULT = "MyPreferencesFile";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);

Button ScanOut=(Button)findViewById(R.id.scanout);
ScanOut.setOnClickListener(this);

Button ScanIn=(Button)findViewById(R.id.scanin);
ScanIn.setOnClickListener(this);

Button EndSession = (Button) findViewById(R.id.endsession);
EndSession.setOnClickListener(this);
}


@Override
public void onClick(View v) {

if(v.getId()==R.id.scanout){

isScanout = true;
IntentIntegrator.initiateScan(this);
}

else if(v.getId()==R.id.scanin){

isScanout = false;
IntentIntegrator.initiateScan(this);

}
else if(v.getId()==R.id.endsession){
Intent endsessionintent = new Intent(MainMenu.this, MainActivity.class);
startActivity(endsessionintent);
}
else if(v.getId()==R.id.toolDB){
Intent i = new Intent(MainMenu.this, ToolsTableLayout.class);
startActivity(i);
}

}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case IntentIntegrator.REQUEST_CODE: {
if (resultCode != RESULT_CANCELED) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

if (scanResult != null) {
String qrCode = scanResult.getContents();

SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
SharedPreferences.Editor editor = codeHack.edit();
editor.putString("entry", qrCode);
editor.commit();
}
}
}
}
}


@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;
}
}

R.id.toolDB是没有反应的按钮..

这是 ToolsTableLayout.java 类(未打开):

public class ToolsTableLayout extends Activity {

public static final String SCAN_RESULT = "MyPreferencesFile";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tablelayout2);

SharedPreferences codeHack = getSharedPreferences(SCAN_RESULT,0);
String QRcode = codeHack.getString("entry", "unregistered");

StringTokenizer token = new StringTokenizer(QRcode," ");
String name = token.nextToken();
String quantity = token.nextToken();

TextView t1 = (TextView) findViewById(R.id.slot1b);
TextView t2 = (TextView) findViewById(R.id.slot2b);
TextView t3 = (TextView) findViewById(R.id.slot3b);

ToolDB info = new ToolDB(this);
info.open();
String c1 = info.getRowID();
info.createEntry(name, quantity);
info.close();

t1.setGravity(Gravity.CENTER_HORIZONTAL);
t2.setGravity(Gravity.CENTER_HORIZONTAL);
t3.setGravity(Gravity.CENTER_HORIZONTAL);

t1.setText(c1);
t2.setText(name);
t3.setText(quantity);
}

}

不确定是否需要 XML 文件?如果是这样,请告诉我,我会更新。

最佳答案

您没有为第四个按钮设置 onClickListener。请添加

Button btnToolDb = (Button) findViewById(R.id.toolDB);
btnToolDb.setOnClickListener(this);

关于java - Android:按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22411021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com