android 组件不可见,Android setVisibility(View.VISIBLE)不显示该组件
我正在使用setVisibility()隐藏onStart()中的一些UI组件,目的是在onActivityResult()的特定条件下使它们重新出现。
我已将变量设置为全局变量,并将其分配给组件onCreate()。
使组件不可见的代码正常工作,例如auth_btn.setVisibility(View.INVISIBLE);
但是,在onActivityResult()处,auth_btn.setVisibility(View.VISIBLE);不会使按钮重新出现。
代码(来自评论中的pastebin):
private Button auth_btn = null;
private Button newAcc_btn = null;
private EditText mEdit = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up the window layout
setContentView(R.layout.main);
//instance of database adapter
db = new DBAdapter(this);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
auth_btn = (Button) findViewById(R.id.btn_auth);
mEdit = (EditText)findViewById(R.id.text_username);
newAcc_btn = (Button) findViewById(R.id.btn_newAcc);
//read every entry from database
db.load();
}
@Override
public void onStart() {
super.onStart();
// If BT is not on, request that it be enabled.
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the session
} else {
setupSession();
}
}
private void setupSession () {
//Authenticate
auth_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
out.write(AUTHENTICATE);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
authenticate();
}
});
//Create new account
newAcc_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try{
out.write(NEWACCOUNT);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
newAccount();
}
});
//Scan QR Code
Button scan = (Button) findViewById(R.id.btn_scan);
scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, SCAN_QR_CODE);
}
});
auth_btn.setVisibility(View.INVISIBLE);
newAcc_btn.setVisibility(View.INVISIBLE);
mEdit.setVisibility(View.INVISIBLE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == SCAN_QR_CODE) {
if (resultCode == RESULT_OK) {
//Successful scan
processQR(intent.getStringExtra("SCAN_RESULT"));
//String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); //format of the code
//Toast.makeText(this, contents, Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Scan failed!", Toast.LENGTH_SHORT).show();
}
}
if (requestCode == REQUEST_ENABLE_BT) {
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
setupSession();
} else {
// User did not enable Bluetooth or an error occurred
Log.d(TAG, "BT not enabled");
Toast.makeText(this, "Bluetooth cannot be enabled", Toast.LENGTH_SHORT).show();
finish();
}
}
}
public void processQR (String content) {
String[] contents = content.split(" ");
if (contents.length != 3) {
Log.e(TAG, "Not well formed QR Code");
}
else {
appKey = contents[APPKEY];
macAdd = contents[MACADR];
my_uuid = UUID.fromString(contents[UUID_STR]);
Log.d(TAG, macAdd);
String appName = db.getAppName(appKey);
Log.d(TAG, appName);
if (appName == null)
return;
Toast.makeText(this, appName, Toast.LENGTH_SHORT).show();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(macAdd);
try {
bt = device.createInsecureRfcommSocketToServiceRecord(my_uuid);
bt.connect();
in = bt.getInputStream();
out = bt.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "Set new acc visible");
mEdit = (EditText)findViewById(R.id.text_username);
mEdit.setVisibility(View.VISIBLE);
newAcc_btn.setVisibility(View.VISIBLE);
if (db.appAccounts(appKey).getCount() > 0)
auth_btn.setVisibility(View.VISIBLE);
}
}
android 组件不可见,Android setVisibility(View.VISIBLE)不显示该组件相关推荐
- android布局闪动,设置child.setvisibility(View.Visible)时,Android主屏幕出现效果闪烁问题...
我已经制作了一个示例应用程序,可以在视镜中浏览不同的布局. XML基本上是(伪代码) 在Java代码中 public boolean onTouchEvent(MotionEvent event) c ...
- android连续调用setVisibility(View.VISIBLE)和setVisibility(View.Gone)
最近想利用setVisibility(View.VISIBLE)和setVisibility(View.Gone)来显示和隐藏一个LinearLayout(主要是想当作一个loading界面使用),发 ...
- android imageview.setVisibility(View.VISIBLE)设置无效,代码已执行但是图片不显示
android imageview.setVisibility(View.VISIBLE)设置无效,代码已执行但是图片不显示 在网上找了很多文章,基本上都是说在设置setVisibility()之前, ...
- android中 textview.setVisibility(View.VISIBLE)失效问题
如题,第一次遇见,甚至都觉得这个很奇葩,为什么还会失效,怎么试都不行,就是不显示,后来用了INVISIBLE的方法有用了,但是毕竟是占据空间的 啊,对于效果显示很不友好,于是又找原因,最后经过长达两小 ...
- android 双层旋转菜单,Android 高仿【优酷】圆盘旋转菜单的实现
目前,用户对安卓应用程序的UI设计要求越来越高,因此,掌握一些新颖的设计很有必要. 比如菜单,传统的菜单已经不能满足用户的需求. 其中优酷中圆盘旋转菜单的实现就比较优秀,这里我提供下我的思路及实现,仅 ...
- 项目需求之Android 监听键盘的弹起和隐藏setVisibility(View.GONE)方法失效
最近比较忙咯,因为在做密码密码管理模块,所以一直在跟EditText打交道,我也写了几篇跟EditText有关的博客了,今天又遇到需求了,需要监听系统键盘的弹起和隐藏. 需求大致是这样的: 1.当键盘 ...
- android gone动画_java – Android添加简单的动画,而setvisibility(view.Gone)
我设计了一个简单的布局.我已经完成了设计没有动画,但现在我想添加动画时textview点击事件,我不知道如何使用它. 我的xml设计看起来不错吗? 任何建议,将不胜感激. 我的XML android: ...
- setVisibility(View.INVISIBLE)、android.os.Process.killProcess和System.exit失效
今天遇到一个问题:在拨打电话时,关闭UI,UI又重新被系统拉起了.另外拨打电话时有个imageview会显示,挂断电话会消失,但是挂断后这个imageview没有消失. 一.查setVisibilit ...
- 高德地图组件在Android的应用以及Android与JavaScript的交互(一)
最近在慕课网学习了关于高德地图组件的课程(其实就是一个广告,内容和官网的API完全一样),发现这个JavaScript API比Android API简单方便多了,于是就打算放在Android APP ...
最新文章
- Mac OS X 安装和配置Scala
- 升降压斩波电路matlab,升降压斩波电路matlab仿真.doc
- Paxos与zookeeper
- python编的俄罗斯方块游戏_手把手制作Python小游戏:俄罗斯方块(一)
- 【学术相关】选导师犹如选对象:真真切切,现现实实!
- SVN:冲突解决 合并别人的修改
- 怎么利用迭代器写入mysql_range()是什么?为什么不生产迭代器?
- Asp.net下from认证统一认证配置
- Javascript中的内置对象:RegExp对象
- ASP.NET配置设置-关于web.config各节点的讲解
- html中图片路径的几种使用方式
- Windows操作系统 | Visual C++库可不能乱删
- 如何做一个阿里云物联网安卓原生APP
- 项目整体流程及项目经理工作职责
- 十大管理之项目干系人管理知识点
- 魔窗使用笔记(穿透微信)
- 一文读懂 DNS 解析的工作机制和优化挑战
- 2012网易校园招聘笔试题
- mac 无法打开22端口 无法远程连接ssh 的解决办法
- Excel统计某一列的直方图