我尝试了一切,但无法禁用该保存按钮。我在onCreate结束时写了它。 实际上,我想如果用户编辑一些笔记并从微调器中选择类别,它将自动启用并实现它。 但我希望首先当用户在此页面上时应该禁用按钮。当用户给他的工作量时,它将启用。 但是我应该如何禁用此保存按钮。
这是我的OfflinePBDetails.java类
package passbookManager;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import logs.TraceLog;
import mainApplication.*;
import forbes.mPassbook.R;
import databaseClasses.DBAccountStatement;
import databaseClasses.DBCategoryMaster;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class OfflinePBDetails extends Activity implements OnItemSelectedListener
{
TextView tvDate;
TextView tvAmount;
TextView tvBalance;
TextView tvParticular;
TextView tvTitleAmount;
EditText etNotes;
Spinner spinnerCategory;
Button btnBack;
Button btnSave;
String file_Path;
Properties properties_config;
long sessionTimeout;
String passTransId;
String passDate;
String passParticular;
String passDebitCreditIndicator;
String passAmount;
String passTransString;
String passBalance;
String passFlag;
String transId;
String notes;
int countDBRows;
int countDBColumns;
String dbSubCategory;
String dbNotes;
String selectedCategory;
String[][] tableCategory ;
ArrayList<String> categoryIncome;
ArrayList<String> categoryExpense;
ArrayAdapter<String> adapterSelect;
DBCategoryMaster dbCMaster;
DBAccountStatement dbStatement;
TraceLog tc = new TraceLog();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ActionBar bar=getActionBar();
bar.hide();
setContentView(R.layout.activity_offline_pbdetails);
dbCMaster = new DBCategoryMaster(this);
dbStatement = new DBAccountStatement(this);
file_Path=Environment.getExternalStorageDirectory()+ "/Android/data/forbes.mPassbook/";
readConfigFileDetails();
if( Build.VERSION.SDK_INT >= 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
try
{
tvDate = (TextView)findViewById(R.id.tv_getDate);
tvParticular = (TextView)findViewById(R.id.tv_getParticular);
tvAmount = (TextView)findViewById(R.id.tv_getAmount);
tvTitleAmount = (TextView)findViewById(R.id.tv_titleAmount);
tvBalance = (TextView)findViewById(R.id.tv_getBalance);
spinnerCategory = (Spinner)findViewById(R.id.spn_selectCategory);
etNotes = (EditText)findViewById(R.id.et_getNotes);
btnBack = (Button)findViewById(R.id.btnBack);
btnSave = (Button)findViewById(R.id.btnSave);
Intent intent = getIntent();
passTransId = intent.getExtras().getString("TransactionId");
passDate = intent.getExtras().getString("Date");
passParticular = intent.getExtras().getString("Particular");
passAmount = intent.getExtras().getString("Amount");
passTransString = intent.getExtras().getString("TransString");
passDebitCreditIndicator = intent.getExtras().getString("Indicator");
passBalance = intent.getExtras().getString("Balance");
passFlag = intent.getExtras().getString("FilterFlag");
tvDate.setText(passDate);
tvParticular.setText(passParticular);
tvTitleAmount.setText(passTransString);
if(tvTitleAmount.getText().equals("Withdrawal Amount"))
{
tvAmount.setTextColor(Color.RED);
}
else
{
tvAmount.setTextColor(Color.rgb(48,128,20));
}
String trimAmount = passAmount.trim();
String trimBalance = passBalance.trim();
tvAmount.setText(trimAmount);
tvBalance.setText(trimBalance);
etNotes.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable arg0)
{
btnSave.setEnabled(true);
resetDisconnectTimer();
// Toast.makeText(getApplicationContext(), "After Typing", Toast.LENGTH_LONG).show();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// Toast.makeText(getApplicationContext(), "Before Typing", Toast.LENGTH_LONG).show();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// Toast.makeText(getApplicationContext(), "Typing", Toast.LENGTH_LONG).show();
}
});
btnBack.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(passFlag.equalsIgnoreCase("OfflinePB"))
{
Intent i = new Intent(OfflinePBDetails.this,OfflinePB.class);
i.putExtra("filterFlag","notes");/**********Code to go back to DisplayFilter Page***************************/
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
else if(passFlag.equalsIgnoreCase("notes"))
{
Intent i = new Intent(OfflinePBDetails.this,DisplayFilters.class);
i.putExtra("filterFlag","notes");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
else if(passFlag.equalsIgnoreCase("transfer"))
{
Intent i = new Intent(OfflinePBDetails.this,DisplayFilters.class);
i.putExtra("filterFlag","transfer");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
else if(passFlag.equalsIgnoreCase("cheque"))
{
Intent i = new Intent(OfflinePBDetails.this,DisplayFilters.class);
i.putExtra("filterFlag","cheque");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
}
});
btnSave.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
btnSave.setEnabled(false);
transId = passTransId;
notes = etNotes.getText().toString();
// if(spinnerCategory.length()==0)
// {
// spinnerCategory.setError("Please Enter mPIN ");
// mPin.requestFocus();
// }
// else
// if(notes.trim().length()==0)
// {
// etNotes.setError("Please Add Your Notes ");
// etNotes.setText(null);
// etNotes.requestFocus();
// }
if(dbStatement.updatePassbook(transId, selectedCategory, notes))
{
Toast.makeText(getApplicationContext(), "Data Updated Successfully", Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "selectedCategory : " + selectedCategory, Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "notes : " + notes, Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
tc.WriteToTransactionLog("Exception: OfflinePBDetails.java Save.setOnClickListener():- "+e.toString());
}
}
});
dbGetCategoryList();
btnSave.setEnabled(false);
}
catch(Exception e)
{
tc.WriteToTransactionLog("Exception: OfflinePBDetails.java onCreate():- "+e.toString());
}
}
public void dbGetCategoryList()
{
int element;
try
{
Cursor rs = dbCMaster.getData();
countDBRows = rs.getCount();
countDBColumns = rs.getColumnCount();
tableCategory = new String[countDBRows][countDBColumns];
rs.moveToFirst();
for(element = 0; element < countDBRows; element++)
{
tableCategory[element][0] = rs.getString(0); // subCategory
tableCategory[element][1] = rs.getString(1); // Category
tableCategory[element][2] = rs.getString(2); // transType
rs.moveToNext();
// String finalData = tableCategory[element][0]+ " " + tableCategory[element][1]
// + " " + tableCategory[element][2];
// Toast.makeText(getApplicationContext(), "finalData : " + finalData, Toast.LENGTH_SHORT).show();
}
rs.close();
showCategoryList();
}
catch(Exception e)
{
tc.WriteToTransactionLog("Exception: OfflinePBDetails.java dbGetCategoryList():- "+e.toString());
}
}
public void showCategoryList()
{
int i = 0;
int j = 0;
int element;
String transType;
categoryIncome = new ArrayList<String>();
categoryExpense = new ArrayList<String>();
// categoryIncome = new String[2];
// categoryExpense = new String[3];
try
{
for(element = 0; element < countDBRows; element++)
{
transType = tableCategory[element][2];
// Toast.makeText(getApplicationContext(), "transType : " + transType, Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(), "passDebitCreditIndicator : " + passDebitCreditIndicator, Toast.LENGTH_SHORT).show();
if(passDebitCreditIndicator.equalsIgnoreCase("D"))
{
if(passDebitCreditIndicator.equalsIgnoreCase(transType))
{
if(tableCategory[element][0].equalsIgnoreCase("Others-E"))
{
categoryExpense.add("Others");
}
else
{
categoryExpense.add(tableCategory[element][0]);
}
// Toast.makeText(getApplicationContext(), "categoryExpense : " + categoryExpense, Toast.LENGTH_SHORT).show();
i++;
}
}
else
{
if(passDebitCreditIndicator.equalsIgnoreCase(transType))
{
if(tableCategory[element][0].equalsIgnoreCase("Others-I"))
{
categoryIncome.add("Others");
}
else
{
categoryIncome.add(tableCategory[element][0]);
}
// Toast.makeText(getApplicationContext(), "categoryIncome : " + categoryIncome, Toast.LENGTH_SHORT).show();
j++;
}
}
}
if(passDebitCreditIndicator.equalsIgnoreCase("D"))
{
adapterSelect = new ArrayAdapter<String>(this, R.layout.spinner_pb_details,categoryExpense);
// adapterSelect = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoryExpense);
}
else
{
adapterSelect = new ArrayAdapter<String>(this, R.layout.spinner_pb_details,categoryIncome);
// adapterSelect = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoryIncome);
}
adapterSelect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(adapterSelect);
spinnerCategory.setOnItemSelectedListener(this);
checkDBData();
if(dbSubCategory.equalsIgnoreCase("Others-E") || dbSubCategory.equalsIgnoreCase("Others-I"))
{
dbSubCategory = "Others";
}
ArrayAdapter myAdap = (ArrayAdapter) spinnerCategory.getAdapter(); //cast to an ArrayAdapter
int spinnerPosition = myAdap.getPosition(dbSubCategory);
//set the default according to value
spinnerCategory.setSelection(spinnerPosition);
etNotes.setText(dbNotes);
// to disable save button after notes is filled with note from db and item is selected from item listner
}
catch(Exception e)
{
tc.WriteToTransactionLog("Exception: OfflinePBDetails.java showCategoryList():- "+e.toString());
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// try
// {
spinnerCategory.setSelection(position);
selectedCategory = (String) spinnerCategory.getSelectedItem();
if(passDebitCreditIndicator.equalsIgnoreCase("D"))
{
if(selectedCategory.equalsIgnoreCase("Others"))
{
selectedCategory = "Others-E";
}
// else
// {
// selectedCategory = selectedCategory;
// }
// Toast.makeText(getApplicationContext(), "selectedCategory : " + selectedCategory, Toast.LENGTH_SHORT).show();
}
else
{
if(selectedCategory.equalsIgnoreCase("Others"))
{
selectedCategory = "Others-I";
}
// else
// {
// selectedCategory = (String) spinnerCategory.getSelectedItem();
// }
// Toast.makeText(getApplicationContext(), "selectedCategory : " + selectedCategory, Toast.LENGTH_SHORT).show();
}
btnSave.setEnabled(true);
// switch (position) {
// case 0:
// // Whatever you want to happen when the first item gets selected
// break;
// case 1:
// // Whatever you want to happen when the second item gets selected
// break;
// case 2:
// // Whatever you want to happen when the thrid item gets selected
// break;}
// }
// catch(Exception e)
// {
// Toast.makeText(getApplicationContext(), "Exception onItemSelected : " + e, Toast.LENGTH_SHORT).show();
// }
}
public void checkDBData()
{
try
{
int element;
Cursor rs = dbStatement.getCategory(passTransId);
// int countCategoryRow = rs.getCount();
// int countCategoryColumn = rs.getColumnCount();
rs.moveToFirst();
dbSubCategory = rs.getString(13);
dbNotes = rs.getString(14);
rs.moveToNext();
// String reducedNotes = dbNotes.replace("\n", "").replace("\r", "");
//// String str =dbNotes.substring(0, 3);
// Toast.makeText(getApplicationContext(), "reducedNotes : " + reducedNotes, Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
tc.WriteToTransactionLog("Exception: OfflinePBDetails.java checkDBData():- "+e.toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
public void readConfigFileDetails()
{
try
{
properties_config= new Properties();
FileInputStream fis2 = new FileInputStream(file_Path + "config.properties");
properties_config.load(fis2);
sessionTimeout = Long.parseLong(properties_config.getProperty("session_timeout"));
fis2.close();
}
catch(Exception e)
{
e.printStackTrace();
tc.WriteToTransactionLog("Exception: OfflinePBDetails.java readConfigFileDetails():- "+e.toString());
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed()
{
return;
}
public void onRestart()
{
finish();
super.onRestart();
startActivity(new Intent(this, Welcome.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
}
private Handler disconnectHandler = new Handler(){
public void handleMessage(Message msg)
{
}
};
private Runnable disconnectCallback = new Runnable()
{
@Override
public void run()
{
Toast.makeText(getApplicationContext(), "Session Expired", Toast.LENGTH_LONG).show();
System.out.println("Session Expired");
Intent intent = new Intent(getApplicationContext(),Welcome.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
};
public void resetDisconnectTimer()
{
disconnectHandler.removeCallbacks(disconnectCallback);
disconnectHandler.postDelayed(disconnectCallback, sessionTimeout);
}
public void stopDisconnectTimer()
{
disconnectHandler.removeCallbacks(disconnectCallback);
}
@Override
public void onUserInteraction()
{
resetDisconnectTimer();
}
@Override
public void onResume()
{
super.onResume();
resetDisconnectTimer();
}
@Override
public void onStop()
{
super.onStop();
stopDisconnectTimer();
}
}
这是我的.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bbk_base_all"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingRight="3dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/txtwelcome" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="130dp"
android:gravity="center" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="@drawable/buttonborder"
android:paddingTop="10dp" >
<TextView
android:id="@+id/tv_titleDate"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="25dp"
android:text="Transaction Date"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/tv_getDate"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="5dp"
android:paddingLeft="30dp"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonborder"
android:paddingTop="10dp" >
<TextView
android:id="@+id/tv_titleParticulars"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="25dp"
android:text="Particulars"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/tv_getParticular"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:ems="50"
android:gravity="left"
android:maxLines="4"
android:paddingLeft="30dp"
android:text="Particulars"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</TableRow>
<TableRow
android:layout_width="250dp"
android:layout_height="30dp"
android:background="@drawable/buttonborder"
android:paddingTop="10dp" >
<TextView
android:id="@+id/tv_titleAmount"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="25dp"
android:text="Amount"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/tv_getAmount"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="30dp"
android:text="Amount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</TableRow>
<TableRow
android:layout_width="250dp"
android:layout_height="30dp"
android:background="@drawable/buttonborder"
android:paddingTop="10dp" >
<TextView
android:id="@+id/tv_titleBalance"
android:layout_width="150dp"
android:layout_height="25dp"
android:gravity="left"
android:paddingLeft="25dp"
android:text="Balance Amount"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/tv_getBalance"
android:layout_width="125dp"
android:layout_height="25dp"
android:gravity="left"
android:paddingLeft="30dp"
android:text="Balance"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</TableRow>
<TableRow
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="@drawable/buttonborder"
android:paddingTop="10dp" >
<TextView
android:id="@+id/tv_titleCategory"
android:layout_width="150dp"
android:layout_height="25dp"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="Select Category"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold|italic" />
<Spinner
android:id="@+id/spn_selectCategory"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="30dp"
android:textColor="#FFFFFF" />
</TableRow>
<TableRow
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="@drawable/buttonborder"
android:paddingTop="10dp" >
<TextView
android:id="@+id/tv_titleNotes"
android:layout_width="150dp"
android:layout_height="25dp"
android:gravity="center_vertical"
android:paddingLeft="25dp"
android:text="Notes"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/et_getNotes"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tableLayout1"
android:layout_toLeftOf="@+id/imageView1"
android:hint="Add Your Own Notes"
android:maxLines="3"
android:paddingLeft="30dp"
android:textColor="#FFFFFF" >
</EditText>
</TableRow>
</TableLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:gravity="center"
android:text="Transaction Details"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textSize="24dp"
android:textStyle="bold|italic" />
<Button
android:id="@+id/btnSave"
android:layout_width="110dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tableLayout1"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/normalbuttonborder"
android:text="Save"
android:textSize="16sp"
android:textStyle="bold"
android:enabled="false" />
<Button
android:id="@+id/btnBack"
android:layout_width="110dp"
android:layout_height="30dp"
android:layout_alignBaseline="@+id/btnSave"
android:layout_alignBottom="@+id/btnSave"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@+id/imageView1"
android:background="@drawable/normalbuttonborder"
android:text="Back"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
请帮帮我。
答案 0 :(得分:4)
最初在Button
之后禁用findViewById
btnSave = (Button)findViewById(R.id.btnSave);
btnSave.setEnabled(false);
问题是您Spinner
启用了Button
。默认情况下,自动选择第一项Spinner,并在onCreate(...)中调用,Button
启用,您可以执行的操作
更改允许Button
Spinner's OnItemSelected(...)
EditText's
检查先前选择的值和当前选择的值是否相等且Button
值未更改的逻辑,否则禁用Button
启用boolean
。使用{{1}}标志进行此项工作。
答案 1 :(得分:0)
你没有在启用它之前禁用你的按钮。在声明它之后禁用你的按钮。
btnSave.setEnabled(false);
然后在textChanged方法中启用它
答案 2 :(得分:0)
由于您要禁用按钮使用:
btnSave.setEnabled(false);
或尝试设置:
btnSave.setClickable(false);
以及setEnabled(false)
或者,在xml中使用以下行:
android:clickable = "false"
android:enabled = "false"
每当用户完成笔记编辑(即选择微调项目后),根据你的逻辑启用btnSave上的两个(启用和可点击),即使用:
btnSave.setClickable(true);
btnSave.setEnabled(true);