我试图使用:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1sp"
android:background="#000000"></FrameLayout>
但它最终失败了,因为约束只是变得不稳定。我一直在搜索,但找不到有效的解决方案,而且我正在扯掉我的头发。我的代码如下所示:
button_msg = (Button)findViewById(R.id.button_red);
button_msg.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
greeting = "赤色です" ;
btn_send_msg.callOnClick();
}});
button_msg = (Button)findViewById(R.id.button_blue);
button_msg.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
greeting = "青色です" ;
btn_send_msg.callOnClick();
}});
因此,如果我按下红色,则仅在该按钮周围出现边框(黑色等),反之亦然。它只是直观地表示程序现在处于哪种状态。
编辑:完整代码位于
之下package app.real_time_chat;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private Button add_room;
private EditText room_name;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> list_of_rooms = new ArrayList<>();
private String name;
private DatabaseReference root =
FirebaseDatabase.getInstance().getReference().getRoot();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_room = (Button) findViewById(R.id.btn_add_room);
room_name = (EditText) findViewById(R.id.room_name_edittext);
listView = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,list_of_rooms);
listView.setAdapter(arrayAdapter);
request_user_name();
add_room.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Map<String,Object> map = new HashMap<String, Object>();
map.put(room_name.getText().toString(),"");
root.updateChildren(map);
}
});
root.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
set.add(((DataSnapshot)i.next()).getKey());
}
list_of_rooms.clear();
list_of_rooms.addAll(set);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
i, long l) {
Intent intent = new
Intent(getApplicationContext(),Chat_Room.class);
intent.putExtra("room_name",(
(TextView)view).getText().toString() );
intent.putExtra("user_name",name);
startActivity(intent);
}
});
}
private void request_user_name() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("名前を入れてください:");
final EditText input_field = new EditText(this);
builder.setView(input_field);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
name = input_field.getText().toString();
}
});
builder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
request_user_name();
}
});
builder.show();
}
}
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/pale_blue"
android:layout_height="match_parent"
android:id="@+id/linearlayout">
<ImageView
android:layout_width="382dp"
android:layout_height="62dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:src="@drawable/alumis_logo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="-4dp"
app:layout_constraintHorizontal_bias="0.454" />
<Button
android:id="@+id/button_blue"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:background="#0000ff"
android:text="青色"
app:layout_constraintBottom_toTopOf="@+id/button_green"
app:layout_constraintHorizontal_bias="0.973"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_red"
app:layout_constraintVertical_bias="0.284"
app:layout_constraintVertical_chainStyle="packed"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
<Button
android:id="@+id/button_green"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:background="#00ff00"
android:text="緑色"
app:layout_constraintBottom_toTopOf="@+id/button_yellow"
app:layout_constraintHorizontal_bias="0.973"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_blue"
app:layout_constraintVertical_bias="0.407"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
<Button
android:id="@+id/button_yellow"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="40dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="1dp"
android:text="黄色"
android:background="#ffff00"
app:layout_constraintHorizontal_bias="0.973"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_green"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.04"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
/>
<Button
android:layout_width="129dp"
android:layout_height="48dp"
android:text="Send"
android:id="@+id/btn_send"
tools:layout_constraintTop_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintTop_toTopOf="@+id/msg_input"
app:layout_constraintLeft_toRightOf="@+id/msg_input"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.466"
android:layout_marginTop="-14dp" />
<EditText
android:layout_width="275dp"
android:layout_height="50dp"
android:id="@+id/msg_input"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="1dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="460dp" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="269dp"
android:layout_height="0dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="68dp"
app:layout_constraintBottom_toTopOf="@+id/msg_input"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">
<TextView
android:id="@+id/textView"
android:layout_width="394dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="49dp" />
</ScrollView>
<Button
android:id="@+id/button_red"
android:layout_width="78dp"
android:layout_height="38dp"
android:layout_marginBottom="1dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="60dp"
android:background="#ff0000"
android:text="赤色"
app:layout_constraintBottom_toTopOf="@+id/button_blue"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
在res / drawable文件夹中创建button_border.xml文件
<强> button_border.xml 强>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="@android:color/transparent" >
</solid>
<stroke
android:width="2dp"
android:color="#F06292" >
</stroke>
<corners
android:radius="2dp" >
</corners>
<强> MainActivity.java 强>
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setBackground(getResources().getDrawable(R.drawable.button_border));
}
});