在此布局中,例如,在10次扫描后,我希望按钮说“"发送电子邮件"而不是"扫描qr"并希望它在单击时打开不同的活动(或任何其他行为)。请解释我该怎么做。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_scan"
android:background="@color/mybackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.sdemo.ScanActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scanning QR"
style="@style/myHeading"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/mySubHeading"
android:id="@+id/scan_tips"
android:text="@string/scan_tips"/>
</LinearLayout>
<Button
android:text="Scan QR"
android:layout_width="match_parent"
style="@style/myButton"
android:id="@+id/button_start_scan"
android:onClick="onClick"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
java文件
package com.example.android.sdemo;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScanActivity extends Activity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
mScannerView = new ZXingScannerView(this);
}
public void onClick(View v){
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result){
// Toast.makeText(getApplicationContext(), result.getText(), Toast.LENGTH_SHORT).show();
Intent intentBack = new Intent(getApplication(), StoryActivity.class);
intentBack.putExtra("qrResult", result.getText());
startActivity(intentBack);
//mScannerView.resumeCameraPreview(this);
}
}
答案 0 :(得分:0)
您可以执行以下操作:
声明按钮,全局计数器:
int count = 0;
Button btn;
在onCreate
初始化按钮中:
btn = (Button)findViewById(R.id.button_start_scan);
修改onClick()
方法,如下所示:
public void onClick(View v){
if(count<10){
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
else{
btn.setText("Send Email");
// send mail here
}
count++;
}
希望这有帮助。
答案 1 :(得分:0)
在onclicklistener中执行此操作
Button bt=(Button)view;
bt.seText("Clicked!");