我是Android App Development的新手。我想知道在用户点击屏幕的任何地方我是否有任何方式可以看到button
。我的朋友告诉我,我可能不得不做一些自定义用户界面,但我不知道Android的含义以及如何实现它。我已经知道它应该如何运作。我在想这样的事情:
在XML中:
Create a button with, android:onclick = "makeVisible" and visibility="invisible" attributes
然后在Java代码中:
public void makeVisible (*something goes in here*)
{
button.setVisibility="visible"
}
我不确定你是否会这样做,但这就是我想象的(至少是为了让它出现)我不知道你会如何移动它。请查看图片以获取更多信息。
仅当单击某个菜单选项时,才会显示白色方块。只有在显示该正方形时,我才希望在点击某些内容时显示“添加文本”按钮。
例如:按钮将不可见。如果用户点击任何位置(1,2,3,4,5,6,7,8,9或10),该按钮就会出现在该位置,如果点击了新位置,则该按钮会消失并重新出现在新的位置。
我希望有人可以帮助我。如果我在任何时候都困惑任何人,请不要犹豫要求我澄清一些事情。
感谢您的帮助。
这是我当前的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.android.postvu.MainActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/background"
android:scaleType="centerCrop"
android:src="@drawable/grid"
tools:layout_editor_absoluteY="0dp"
android:layout_marginRight="7dp"
android:layout_marginEnd="7dp"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="@string/image_text_editor"
android:textColor="@android:color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Image Text Editor"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:onClick="myOnClickMethod"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.98"
android:id="@+id/button"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="450dp"
android:layout_height="450dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/white_background"
android:tag="white"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/imageView2"
app:layout_constraintRight_toRightOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/textView"
app:srcCompat="@mipmap/white" />
<Button
android:id="@+id/add_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_text"
android:visibility="visible"
android:onClick="makeVisible"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
我目前的Java代码:
package com.example.android.postvu;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.SubMenu;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
view = (ImageView)findViewById(R.id.imageView3);
backgroundImageName = String.valueOf(view.getTag());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void myOnClickMethod(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
final int CONTEXT_MENU_VIEW = 1;
final int CONTEXT_MENU_EDIT = 2;
final int MENU_SORT = 3;
final int MENU_SORT_BY_NAME = 4;
final int MENU_SORT_BY_ADDRESS = 5;
ImageView view;
String backgroundImageName;
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Options");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Take Photo");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Photo Album");
SubMenu sub=menu.addSubMenu(Menu.NONE, MENU_SORT, Menu.NONE, "Plain Image");
sub.add(Menu.NONE, MENU_SORT_BY_NAME, Menu.NONE, "GridVu");
sub.add(Menu.NONE, MENU_SORT_BY_ADDRESS, Menu.NONE, "Story");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case CONTEXT_MENU_VIEW:
return true;
case CONTEXT_MENU_EDIT:
return true;
case MENU_SORT_BY_NAME:
if (backgroundImageName.equals("white"))
{
view.setVisibility(View.VISIBLE);
}
return true;
case MENU_SORT_BY_ADDRESS:
return true;
default:
return super.onContextItemSelected(item);
}
}
public void makeVisible(View w) {
View b = findViewById(R.id.add_text);
b.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:1)
<Button
android:id="@+id/add_text"
...
android:visibility="visible" <!-- This is already visible -->
android:onClick="makeVisible"
... />
您的意思是使用不同的视图ID吗?也许imageView3
?该按钮已经可见,但您有正确的想法
public void makeVisible(View w) {
View b = findViewById(R.id.add_text); // Change which view
b.setVisibility(View.VISIBLE);
}
顺便说一下,View w
将等于您点击的按钮。无需再找到它
答案 1 :(得分:1)
您可以为父布局添加触控侦听器,在您的情况下为ConstraintLayout。
NSString *strInputDateString = @"2017-07-31 02:18:50";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
//Set new dateFormate
NSDate *date1 = [dateFormat dateFromString:strInputDateString];
[dateFormat setDateFormat:@"dd-MM-YYYY hh:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"CST"]];
NSString *strOutputDateString = [dateFormat stringFromDate:date1];
NSLog(@"%@",strInputDateString);
NSLog(@"%@",strOutputDateString);
使用x和y坐标动态添加按钮,如下所示
parent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//get x and y with event.getX() and event.getY()
return true;
}
});
希望它有所帮助!
答案 2 :(得分:0)
您可以在活动视图中的主xml标记上定义ID,然后通过在视图上应用setOnclickListener方法来播放屏幕。 要使按钮可见,您可以设置&#34; setVisibiliy(View.VISIBLE)&#34;。
实施例: - 在你的xml和你的Java中:
View main_view = findViewById(R.id.main_view);
Button button = ...
main_view.setOnClickListener(new OnClickListener{
@Override
public void onClick(View v){
if(button.getVisibility() == View.INVISIBLE){
button.setVisibiliy(View.VISIBLE);
}else{
button.setVisibiliy(View.INVISIBLE);
}
}
});